Terraform multi map in for_Each loop in creating resources for zone and records

Hi , Any help is much appreciated , what i am trying to do is , i have 2 domain for which i try to created hosted zone and with the created hosted zone , i need to take the zone id and try to A record , that is where i have issue passing the zone id in a loop for another resource

my tfvars




facade_hostname = [
    "mobiel2.t.bankvanmorgen.nl" , #CHANGE-13925
    "portaalapi.testing.knab.nl" #CHANGE-16462
]



dns_config = [
      {
        name    = "mobiel2.t.bankvanmorgen.nl",
        ttl     = "5",
        type    = "A",
        records = [
          "172.29.177.22"]
      }   
    ,
     {
        name    = "portaalapi.testing.knab.nl",
        ttl     = "5",
        type    = "A",
        records = [
          "172.29.177.7"]
      }
    
]

my variables.tf

variable "facade_hostname" {
  type = list
  default = []
}

variable "dns_config"{
  type = list
  default = []
}

my resource.tf file


resource "aws_route53_zone" "private" {
  for_each =  var.facade_hostname
  name          = each.value
  force_destroy = true
  vpc {
    vpc_id = module.vpc_private.vpc_id
  }
}



resource "aws_route53_record" "A" {
  for_each = aws_route53_zone.private
  zone_id = each.value.zone_id
  name    = each.value.name
  ttl     = each.value.ttl
  type    = each.value.type
  records = each.value.records

  allow_overwrite = true
}

Error i am getting

 Error: Invalid "each" attribute
│
│   on route53.tf line 13, in resource "aws_route53_record" "A":
│   13:   name    = each.name
│
│ The "each" object does not have an attribute named "name". The supported
│ attributes are each.key and each.value, the current key and value pair of
│ the "for_each" attribute set.
╵
╷
│ Error: Invalid "each" attribute
│
│   on route53.tf line 14, in resource "aws_route53_record" "A":
│   14:   ttl     = each.ttl
│
│ The "each" object does not have an attribute named "ttl". The supported
│ attributes are each.key and each.value, the current key and value pair of
│ the "for_each" attribute set.

I am trying as much i can but Any help is much appreciated ,

Hi @dilip.deenadayalan,

The snippet of code shown in the error messages you shared doesn’t match the code example you showed above.

The error message says that resource "aws_route53_record" "A" contains the following:

  name    = each.name

But the corresponding line in your example is:

  name    = each.value.name

The second of these is the correct way to write it: you are accessing the name attribute of the current object in each.value.

Hi @apparentlymart , Thanks for the message , i fixed it , thanks

resource "aws_route53_zone" "private" {
  for_each      = var.mobile_facade_hostname
  name          = each.key
  force_destroy = true
  vpc {
    vpc_id = module.vpc_private.vpc_id
  }
}

resource "aws_route53_record" "A" {
  for_each = aws_route53_zone.private
  zone_id  = each.value["zone_id"]
  name     = trimsuffix(each.value["name"], ".")
  type     = "A"
  ttl      = "5"
  records  = [var.mobile_facade_hostname[trimsuffix(each.value["name"], ".")]]

My tfvars

mobile_facade_hostname  = { "x.y.nl" = "1.2.3.4", "a.b.nl" = "5.6.7.8" }

variables.tf

variable "mobile_facade_hostname" {
  type    = map(string)
  default = {}
}