jadejoe
jadejoe

Reputation: 753

How to turn a nested array in Terraform

How can I retrieve the nested arrays with map? I want to pass the records to be registered to Route53 from local.

example

locals {

  cname = {
    "bbb.example.io" = {
      records = ["example"]
    }
    "aaa.example.io" = {
      records = [
        "example1",
        "example2",
        "example3"
      ]
    }
  }
}

resource "aws_route53_record" "cname" {
  for_each = local.cname
  name     = each.key
  records  = [] ←Problem areas
  ttl      = 300
  type     = "CNAME"
  zone_id  = aws_route53_zone.example.zone_id
}

Upvotes: 0

Views: 866

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74779

Given the way you've set up the for_each in this example, each.value in that block will be the corresponding object from your local.cname map, so you can access the records attribute of that object in the usual way:

resource "aws_route53_record" "cname" {
  for_each = local.cname
  name     = each.key
  records  = each.value.records
  ttl      = 300
  type     = "CNAME"
  zone_id  = aws_route53_zone.example.zone_id
}

Upvotes: 1

Related Questions