Home > Software design >  Using default VPC id to retrieve specific subnet in terraform not working
Using default VPC id to retrieve specific subnet in terraform not working

Time:03-07

I'm trying to get the subnet of my RDS module/resource that is deployed to the default VPC. The RDS instance is deployed to ap-southeast-2c.

My configuration file to retrieve the subnet is below.

data "aws_vpc" "default_vpc_data" {
  default = true
}

data "aws_subnet" "selected_subnet" {
  id                = data.aws_vpc.default_vpc_data.id
  availability_zone = "ap-southeast-2c"
}

I'm getting the following error when trying to apply the file.

Error: no matching EC2 Subnet found with data.aws_subnet.selected_subnet on glue_connections.tf line 17, in data "aws_subnet" "selected_subnet":

I'm not sure why this is happening?

CodePudding user response:

You should be using aws_subnets instead:

data "aws_subnets" "selected_subnet" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.default_vpc_data.id]
  }
  
  filter {
    name = "availability-zone"
    values = ["ap-southeast-2c"]
  }
}
  • Related