Convert route_53_zone zone_ids to csv

Looking to get the resultant zone_id (s) from created resources as a csv to be written to SSM parameter store

–variables.tf–
phz_name = ["abc.company.com", "def.company.com"]

–main.tf–

resource "aws_route53_zone" "private" {
  for_each = toset(var.phz_names)
  name     = each.value
  vpc { vpc_id = aws_vpc.main.id }
}

The zones create successfully, but not able to figure out how to join them into a comma delimited string such as Z0923409AS,Z2390909400,Z034090943

Hi @ellde,

Assuming that the those Z-prefixed strings you shared are from the id attribute of aws_route53_zone, you could generate a string like that with an expression like this:

join(",", [for z in aws_route53_zone.private : z.id])

This expression has two main parts:

  • The [for ... in ... : ...] is a for expression, used for constructing one collection from another. In this case it visits each element of aws_route53_zone.private – which is a map of aws_route53_zone objects – and takes its id attribute, collecting all of the results into a list (due to the [ ] brackets).
  • The join function takes a list of strings and concatenates all of the elements of that list together using a given delimiter, which in this case is ",".

The result should be a string like the one you included in your question. You can place this expression anywhere that Terraform expects an expression returning a string.

1 Like

awesome thanks @apparentlymart this worked perfectly