Remote state data source with periods in key name

115 views
Skip to first unread message

Matthew Ceroni

unread,
Jun 27, 2018, 2:05:36 PM6/27/18
to Terraform
Related to this issue (I believe): https://github.com/hashicorp/terraform/issues/11534

I create multiple route53 zones, and output the zone name to zone ID map via a remote state (ie: domain.com => Z343423FND). The problem is that the key contains a period and upon lookup I get "key ... does not exists in map ...".

The solution would be to replace all . with something else, like -. However with the available variable interpolation functions I haven't been able to figure out how to do this. 

output "public_zone_map" { value  = "${zipmap(aws_route53_zone.public.*.name, aws_route53_zone.public.*.zone_id)}" }


Because I can't loop over all the resources and do a search and replace. 

Maybe the community can help me with some magic terraform code to either solve the problem and allow periods to be in map key names or do the search and replace.

Thanks

Matthew Ceroni

unread,
Jun 27, 2018, 2:29:27 PM6/27/18
to Terraform
I came up with a solution I think might work, just running into an output issue

resource aws_route53_zone "public" {
  count = "${length(var.route53_zone_name) * var.create_public_zone}"

  name = "${format("%s.", element(var.route53_zone_name, count.index))}"

  tags = "${merge(var.tags, local.tags, map("visibility", "public"))}"
}

/* replace key names */
data null_data_source "public_zone_map" {
  count = "${length(aws_route53_zone.public.*.name)}"

  inputs = {
    key  = "${replace(replace(element(aws_route53_zone.public.*.name, count.index), ".", "-"), "/-$/", "")}"
  }
}


So you can see here I use the null_data_source to loop over the name attribute of all aw_route53_zone resources.

The issue I run into now is on output:

output "test" {
  value = [ "${data.null_data_source.public_zone_map.*.outputs["key"]}" ]
}

Which results in the following error:

Error: Error refreshing state: 1 error(s) occurred:

* module.vpc-dns.output.test: __builtin_StringToInt: strconv.ParseInt: parsing "key": invalid syntax in:

${data.null_data_source.public_zone_map.*.outputs["key"]}

If the null_data_source is not a list this works. To me it appears that terraform is interpreting "key" as a list index, which it is not. 

Matthew Ceroni

unread,
Jun 27, 2018, 2:57:13 PM6/27/18
to Terraform
Ended up using a null_resource instead. As the outputs from that are not a map. They are just access like any other attribute from a resource. Basically the same code, just use null_resource instead. 

resource null_resource "private_zone_map" {
  count = "${length(aws_route53_zone.vpc.*.name)}"

  triggers = {
    key  = "${replace(replace(element(aws_route53_zone.vpc.*.name, count.index), ".", "-"), "/-$/", "")}"
  }
}

resource null_resource "public_zone_map" {
  count = "${length(aws_route53_zone.public.*.name)}"

  triggers = {
    key  = "${replace(replace(element(aws_route53_zone.public.*.name, count.index), ".", "-"), "/-$/", "")}"
  }
}

output "private_zone_map" { value = "${zipmap(null_resource.private_zone_map.*.triggers.key, aws_route53_zone.vpc.*.zone_id)}" }
output "public_zone_map" { value  = "${zipmap(null_resource.public_zone_map.*.triggers.key, aws_route53_zone.public.*.zone_id)}" }



Reply all
Reply to author
Forward
0 new messages