Jakund
Jakund

Reputation: 211

Terraform get a list of IDs from a resource created with count

I'm defining a number of subnet resources:

resource "aws_subnet" "my_subnets" {
  count = 8
  cidr_block = cidrsubnet(var.cidr_block, 3, count.index)
  vpc_id = var.vpc
}

I then have to pass a list of those subnet IDs to another resource. I know that the IDs are reachable on aws_subnet.my_subnets[count].id, but how do I loop through those and append all of the values to a list in order to pass it to the other resource? The recommendation I've seen is to tag the subnets, then use a data attribute to look up those subnets, and they will be returned in a list format, but I have the IDs right there on the output of the resource.

Upvotes: 9

Views: 19347

Answers (1)

Jakund
Jakund

Reputation: 211

Found what I was thinking of - splat expression: https://www.terraform.io/docs/language/expressions/splat.html

Upvotes: 9

Related Questions