Home > other >  Terraform data filter : get multiple values
Terraform data filter : get multiple values

Time:04-25

I want to get subnet values from another repository. For this purpose I added data aws_subnet part. However I'm having a problem with the filtering part. At the end of the values line I need to count for each subnets. I tried to use count.index and different things. But I get this error: The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set. So how can I use * for the filter values part. Such as: ${var.vpcname}-Public-*

My Subnets:

myvpc-Private-0
myvpc-Private-1
myvpc-Private-2
myvpc-Public-0
myvpc-Public-1
myvpc-Public-2

My data part:

data "aws_subnet" "public" {
  filter {
    name   = "tag:Name"    
    values = ["${var.vpcname}-Public-"]
  }
}

data "aws_subnet" "private" {
  filter {
    name   = "tag:Name"
    values = ["${var.vpcname}-Private-"]
  }
}

Want to see all subnets with below output part.

output "private" {
  value = data.aws_subnet.private.*.id
}

output "public" {
  value = data.aws_subnet.public.*.id
}

CodePudding user response:

You should be using aws_subnets, not aws_subnet:

data "aws_subnets" "public" {
  filter {
    name   = "tag:Name"    
    values = ["${var.vpcname}-Public-*"]
  }
}

data "aws_subnets" "private" {
  filter {
    name   = "tag:Name"
    values = ["${var.vpcname}-Private-*"]
  }
}

then

output "private" {
  value = data.aws_subnets.private.ids
}

output "public" {
  value = data.aws_subnets.public.ids
}
  • Related