CodexBloom - Programming Q&A Platform

Terraform 1.4.3: implementing Managing IAM Policies Using Dynamic Blocks for AWS

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-08-21
terraform aws iam hcl

I've encountered a strange issue with I've been banging my head against this for hours. I'm relatively new to this, so bear with me. I'm stuck on something that should probably be simple... I'm trying to manage IAM policies for an AWS Lambda function using Terraform 1.4.3, but I'm running into issues with dynamic blocks. My goal is to create policies based on a list of permissions that should be assigned to the Lambda function. However, I'm getting an behavior that states: `behavior: Invalid function argument: The argument "for_each" must be a map or a set of strings`. Here is the relevant code snippet where I define the IAM policy: ```hcl variable "lambda_permissions" { type = list(string) default = ["s3:GetObject", "dynamodb:PutItem"] } resource "aws_iam_policy" "lambda_policy" { name = "lambda_policy" description = "A policy for Lambda permissions" policy = jsonencode({ Version = "2012-10-17" Statement = [ for perm in var.lambda_permissions : { Effect = "Allow" Action = perm Resource = "*" } ] }) } ``` I attempted using a for expression within the `Statement` block, but it seems Terraform isn't recognizing my list of permissions correctly. I’ve also tried converting the list to a set with `to_set(var.lambda_permissions)`, but I still faced the same behavior. I've looked at some documentation and examples related to dynamic blocks, but it appears that I might be misusing them or missing something important. Any guidance or alternative approaches would be greatly appreciated! My development environment is Ubuntu. How would you solve this? What would be the recommended way to handle this? This issue appeared after updating to Hcl stable. My development environment is macOS.