Terraform outputs with count.index

Hi @lpossamai,

The expression aws_route53_zone.private refers to a list of zero or one items, so in order to return a single ID in this output value you need to decide what the value ought to be in the case where there are no instances of the resource.

A common choice is to make it be null in that case, and so Terraform has a built-in function one to help deal with that situation in a concise way:

output "route53_private_prod" {
  value = one(aws_route53_zone.private[*].zone_id)
}

The expression above first uses the splat operator to transform the list of objects into a list of just zone_id values, which will also have either zero or one elements depending on the resource count.

The one function then deals with the two cases:

  • If the list has only one value, it’ll return that value.
  • If the list has no values, it’ll return null to represent the absense of a value.
6 Likes