Search code examples
aws-lambdaterraform

Use a loop to create multiple source_access_configuration resources in terraform


I have a variable (type: list of string) for external subnets. For e.g., external_subnets = ["subnet-1", "subnet-2", "subnet-3"]. The list and count could vary based on the environment. I would like to create source_access_configuration for lambda event source mapping for kafka for these subnets.

I tried the below and it gives me Unexpected attribute: An attribute named "for_each" is not expected here. Any suggestions?

source_access_configuration {
  for_each = var.external_subnets

  type = "VPC_SUBNET"
  uri  = "subnet:${each.value}"
}

Solution

  • It appears from your question that you are attempting to implement a dynamic block. In your situation this could be performed like:

    dynamic "source_access_configuration" {
      for_each = var.external_subnets
    
      content {
        type = "VPC_SUBNET"
        uri  = "subnet:${source_access_configuration.value}"
      }
    }