CodexBloom - Programming Q&A Platform

Terraform: solution with Dynamic Block for IAM Policies Not Applying Correctly

👀 Views: 26 💬 Answers: 1 📅 Created: 2025-06-08
terraform aws iam hcl

I'm a bit lost with I'm trying to use a dynamic block in my Terraform configuration for creating IAM policies, but it appears that the policies are not being applied as expected. I have upgraded to Terraform version 1.4.0 and am using the AWS provider version 4.0.0. Here’s a simplified version of my setup: ```hcl variable "allowed_actions" { type = list(string) default = ["s3:ListBucket", "s3:GetObject"] } resource "aws_iam_policy" "my_policy" { name = "MyPolicy" description = "A test policy for S3 access" policy = jsonencode({ Version = "2012-10-17" Statement = [ for action in var.allowed_actions : { Effect = "Allow" Action = action Resource = "*" } ] }) } ``` When I run `terraform apply`, I see the policy being created, but only the first action in the list is being applied. The output of the policy reflects only one statement: ```json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "*" } ] } ``` I've tried changing the `for` loop to a `count` argument, but that led to a failed validation behavior. Also, I confirmed that `allowed_actions` is being populated correctly by outputting its value. Here’s what I see when I run `terraform output`: ```hcl allowed_actions = ["s3:ListBucket", "s3:GetObject"] ``` I also checked the Terraform documentation and made sure my syntax aligns with the examples for dynamic blocks, but I still can’t figure out why the second action isn’t being included. Any insights on what might be going wrong or how to troubleshoot this scenario further? I'm open to any suggestions. My development environment is Linux. What would be the recommended way to handle this?