Data.tf
Data file will store all the general dynamic blocks of your Terraform project. They are usually fetched using the cloud providers API.
They can be used with a prefix of data.
i.e. data.aws_vpc
data.tf
file
- These configurations are done in the
data.tf
file.
Public subnet
- Create
data
block type ofaws_subnet_ids
namedpublic
vpc_id
should bevar.vpc_id
- To get the correct public subnets we need to use filters and since our subnets have tag
type: public/private
we can utilise that.
filter { name = "tag:type" values = ["public"] }
Private subnet
- Create
data
block type ofaws_subnet_ids
namedprivate
vpc_id
should bevar.vpc_id
- To get the correct public subnets we need to use filters and since our subnets have tag
type: public/private
we can utilise that.filter { name = "tag:type" values = ["private"] }
Answer
terraform/data.tf
data "aws_subnet_ids" "public" {
vpc_id = var.vpc_id
filter {
name = "tag:type"
values = ["public"]
}
}
data "aws_subnet_ids" "private" {
vpc_id = var.vpc_id
filter {
name = "tag:type"
values = ["private"]
}
}