- I'm creating a list of IAM policy, which is stored in .json format.
- I have only 1 resource block, by using
count = length(count)
I want to create a multiple IAM policy. - The policies are stored in .json format. I am referring to them in Terraform by using
file()
.
If I create a multiple resources block, the program works.
main.tf:
resource "aws_iam_role_policy" "cloudcheckr" {
count = "${length(var.file_name)}"
role = "${aws_iam_role.cloudcheckr.id}" // An IAM role is created in another resource block
name = "${var.file_name[count.index]}"
policy = "${file("${var.file_name[count.index]}.json")}"
variables.tf:
variable "file_name" {
type = "list"
default = [
"xxxxxx",
"xxxxxx",
"xxxxxx",
"xxxxxx",
]
}
Expected results:
Multiple IAM policies are created.
Actual results:
aws_iam_role_policy.cloudcheckr: 3 error(s) occurred:
* aws_iam_role_policy.cloudcheckr[3]: file: open iam_policy_cloudcheckr_security.json: no such file or directory in:
${file("${var.file_name[count.index]}.json")}
* aws_iam_role_policy.cloudcheckr[0]: file: open iam_policy_cloudcheckr_cloudwatchflowlogs.json: no such file or directory in:
${file("${var.file_name[count.index]}.json")}
* aws_iam_role_policy.cloudcheckr[2]: file: open iam_policy_cloudcheckr_inventory.json: no such file or directory in:
${file("${var.file_name[count.index]}.json")}