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.tffile.
Public subnet
- Create
datablock type ofaws_subnet_idsnamedpublicvpc_idshould bevar.vpc_id- To get the correct public subnets we need to use filters and since our subnets have tag
type: public/privatewe can utilise that.
filter { name = "tag:type" values = ["public"] }
Private subnet
- Create
datablock type ofaws_subnet_idsnamedprivatevpc_idshould bevar.vpc_id- To get the correct public subnets we need to use filters and since our subnets have tag
type: public/privatewe 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"]
}
}