Issue with looping over a set using remote state

So, I have a variable that contains a list of strings that map to subdomains. These produce outputs of their ns servers in remote state. Now, what I’m trying to do is do a terraform apply on the root account to create ns records so that the subdomains work.

My variable is setup like this:
sub_domains = [

While my resource block is setup like this:

resource "aws_route53_record" "subns" {
    for_each = toset(var.sub_domains)
    zone_id = aws_route53_zone.root[0].zone_id
    name    = "${each.key}"
    type    = "NS"
    ttl     = "300"
    records = data.terraform_remote_state.[each.key].outputs.hosted_zone_name_servers

Currently, I’m getting this error:

Error: Invalid attribute name

on line 24, in resource “aws_route53_record” “subns”:
24: records = data.terraform_remote_state.[each.key].outputs.hosted_zone_name_servers

An attribute name is required after a dot.

How can I get this to work?


Hi @thedraketaylor,

You seem to be missing the name of your data resource in the reference. The full address of a data resource is data.resource_type.resource_name, but your data.terraform_remote_state. is missing the resource name part.

You didn’t share your data "terraform_remote_state" block so I can’t tell you exactly what name to put there, but if you had the following block:

data "terraform_remote_state" "example" {
  # ...

…then the correct syntax to refer to it would be:

  records = data.terraform_remote_state.example[each.key].outputs.hosted_zone_name_servers