Terraform is a powerful infrastructure as code (IaC) application. For example, it may be used to deploy an Amazon Virtual Private Cloud (Amazon VPC) along with it’s various components, such as a subnet. Doing so requires you to provide the Amazon VPC ID where the subnet will be deployed.

However, how do you provide Terraform with the VPC ID if it has not already been created? The Terraform solution is to reference the Amazon VPC in the code and below is an example of this (aws_vpc.TERRAFORM-VPC.id). You must specify the service provider and resource type (in this case it is aws_vpc) and append the id attribute.

resource “aws_vpc” “TERRAFORM-VPC” {
cidr_block = “10.0.0.0/16”
enable_dns_support = “true”
enable_dns_hostnames = “true”

tags = {
Name = “VPC-01”
}
}

resource “aws_subnet” “TERRAFORM-SUBNET” {
cidr_block = “10.0.1.0/24”
vpc_id = aws_vpc.TERRAFORM-VPC.id

tags = {
Name = “SUBNET-01”
}
}