2

I have a terraform variable defined like that:

variable "domains" {
  default = {
    instance01 = [
      "example.com",
      "www.example.com",
      "staging.example.com"
    ]
  }
}

Terraform version: Terraform v0.13.3

How can I access the domain names in instance01 and loop over the values?

How can I access the instance names and loop over the instance names?

Edit:

What I tried was creating a loop to iterate over each instance and over each domain name in the list associated to the instance:

resource "aws_lb_listener_certificate" "lb_listener_certs" {
  for_each        = var.domains
  listener_arn    = aws_lb_listener.front_end_https[each.key].arn
  certificate_arn = data.aws_acm_certificate.cert[each.value].arn
}

Error I get is:

Error: Invalid index

  on main.tf line 86, in resource "aws_lb_listener_certificate" "lb_listener_certs":
  86:   certificate_arn = data.aws_acm_certificate.cert[each.value].arn
    |----------------
    | data.aws_acm_certificate.cert is object with 3 attributes
    | each.value is tuple with 3 elements

The given key does not identify an element in this collection value: string
required.

It should create for each key (e.g. instance01) and each child of the list a aws_lb_listener_certificate (e.g. example.com, www.example.com). But I cannot get it to interate over the items of the list assigned to instance01.

I basically want it to make a map like that:

instance01 => example.com
instance01 => www.example.com
instance01 => staging.example.com

I'd like to use this key value pair to create the aws_lb_listener_certificate.

2 Answers 2

0

To get to a domain list use var.domains.instance01. This is the list of the domain.

Iterating over can mean multiple things in Terraform. If you want to transform the list, for example make it uppercase, use a for expression: val = [for domain in var.domains.instance01: upper(domain)]:

[
  "EXAMPLE.COM",
  "WWW.EXAMPLE.COM",
  "STAGING.EXAMPLE.COM",
]

If you want a resource for each of the domains for the instance, you can use a for_each:

resource "res" "r" {
  for_each = var.domains.instance01
  domain = each.key
}

(ref: https://www.terraform.io/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings)

To get the instance names, you can use the key function:

keys(var.domains)
[
  "instance01",
]
0
0

Ok, so I think I got the solution:

variable "domains" {
  default = {
    instance01 = [
      "example.com",
      "www.example.com",
      "staging.example.com"
    ]
  }
}

locals {
  domain-association-list = flatten([
    for instance, domains in var.domains : [
      for domain in domains : {
        instance = instance
        domain = domain
      }
    ]
  ])

  domain-association-map = { for item in local.domain-association-list: 
     values(item)[0] => values(item)[1]
   }
}

resource "aws_lb_listener_certificate" "lb_listener_certs" {
  for_each        = local.domain-association-map
  listener_arn    = aws_lb_listener.front_end_https[each.value].arn
  certificate_arn = data.aws_acm_certificate.cert[each.key].arn
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .