Skip to content

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

  1. These configurations are done in the data.tf file.

Public subnet

  1. Create data block type of aws_subnet_ids named public
    1. vpc_id should be var.vpc_id
    2. 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

  1. Create data block type of aws_subnet_ids named private
    1. vpc_id should be var.vpc_id
    2. 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"]
  }
}