HHJ
HHJ

Reputation: 109

Filters are not filtering subnets in Terraform

It's been my 4th day since I studied Terraform and I had a problem. I have four subnet [ Public-web-Public1, Public-web-Public2, Public-web-Private1, Public-web-Private2] and i want to choose only 2subnets Public-web-Public 1,2 but it didn't work also, I tried using tags instead of filters but result is no matching subnet found too i don't know why? please can you help me?

data "aws_subnet_ids" "test_subnet_ids" {
  vpc_id = "${aws_vpc.vpc1.id}"
}

data "aws_subnet" "test_subnet" {
  count = "${length(data.aws_subnet_ids.test_subnet_ids.ids)}"
        filter {
        name = "tag:Name"
        values = ["Public-web-Pub*"]
        }
  id    = "${tolist(data.aws_subnet_ids.test_subnet_ids.ids)[count.index]}"
}

output "subnet_id" {
  value = "${data.aws_subnet.test_subnet.*.id}"
}

Upvotes: 1

Views: 1771

Answers (1)

Marcin
Marcin

Reputation: 238061

You can just use test_subnet_ids. No need for ``aws_subnet, just to get the ids of your two subnets. But once you get the ids, you can then use aws_subnet` to get full details of each subnet that was returned in your search.

data "aws_subnet_ids" "test_subnet_ids" {

  vpc_id = "${aws_vpc.vpc1.id}"
  
  filter {
    name   = "tag:Name"
    values = ["Public-web-Pub*"]
  }  
  
}

output "subnet_ids" {
  value = data.aws_subnet_ids.test_subnet_ids.ids
}

Upvotes: 1

Related Questions