Error when creating nested for loop in Terraform locals

Viewed 32

this is the terraform var file.

endpoints = [
  {
    domain_name     = "my.domain.com"
    aws_region      = "us-east-2"
    custom_endpoints = {
      service = {
        endpoint_name    = "web",
        health_check_name = "web"
      }
    }
  },
  {
    domain_name = "my2.domain.com"
    aws_region  = "us-west-2"
    custom_endpoints = {
      service = {
        endpoint_name    = "web2",
        health_check_name = "web2"
      }
    }
  }
]

I have the following terraform code.

  custom_endpoints = [ for each in var.endpoints : {
                        for key, value in each.custom_endpoints: 
                         key => {
                           for k, v in value: {
                             "${value.endpoint_name}.${each.domain_name}" => {
                                health_check_fqdn = "${value.health_check_name}.${each.domain_name}"
                                region = each.aws_region
                           }
                          }
                         }
                        }
                     ]

terraform plan produces the following:

Error: Missing key/value separator

  on locals.tf line 13, in locals:
  12:                            for k, v in value: {
  13:                              "${value.endpoint_name}.${each.domain_name}" => {

Expected an equals sign ("=") to mark the beginning of the attribute value.

I update the code so that it uses "=" instead of "=>" as it suggests

  custom_endpoints = [ for each in var.endpoints : {
                        for key, value in each.custom_endpoints: 
                         key => {
                           for k, v in value: {
                             "${value.endpoint_name}.${each.domain_name}" = {
                                health_check_fqdn = "${value.health_check_name}.${each.domain_name}"
                                region = each.aws_region
                           }
                          }
                         }
                        }
                     ]

This now returns a new error.

Error: Invalid 'for' expression

  on locals.tf line 12, in locals:
  11:                          key => {
  12:                            for k, v in value: {
  13:                              "${value.endpoint_name}.${each.domain_name}" = {
  14:                                 health_check_fqdn = "${value.health_check_name}.${each.domain_name}"
  15:                                 region = each.aws_region
  16:                            }
  17:                           }
  18:                          }

Key expression is required when building an object.

I'm confused about what I'm doing wrong. Any input is appreciated.

1 Answers

You have extra pair of {} which is not needed. Also its not exactly clear what do you expect to achieve with your for loops (they have other mistakes), but to make it just work, you can do:

custom_endpoints = [ 
    for idx, each in var.endpoints : {
      for key, value in each.custom_endpoints: 
       "${idx}-${key}" => {
         for k, v in value: 
           "${k}-${value.endpoint_name}.${each.domain_name}" => {
              health_check_fqdn = "${value.health_check_name}.${each.domain_name}"
              region = each.aws_region
         }
       }
      }
   ] 
}
Related