Terraform AWS VPC is a way to automate the creation and management of virtual private clouds in Amazon Web Services using Infrastructure as Code. This approach reduces manual errors and speeds up deployment.
Terraform uses configuration files written in HashiCorp Configuration Language (HCL) to define the desired state of AWS VPC resources. When you run terraform apply, it provisions the VPC, subnets, route tables, and other networking components in AWS.
A basic Terraform AWS VPC configuration includes:
Terraform provides several advantages over manual AWS VPC setup:
For example, a Terraform configuration can create a multi-tier architecture with public and private subnets in minutes, which would take hours manually through the AWS console.
Many users confuse Terraform AWS VPC with other networking concepts:
| Concept | Misconception | Reality |
|---|---|---|
| VPC Peering | Part of basic VPC creation | Requires additional configuration after VPC creation |
| NAT Gateways | Always needed for private subnets | Only required if private subnets need internet access |
| Security Groups | Replace network ACLs | Work alongside network ACLs for different layers of security |
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main-vpc"
}
}
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
availability_zone = element(data.aws_availability_zones.available.names, count.index)
tags = {
Name = "public-subnet-${count.index}"
}
}
This example creates a VPC with two public subnets in different availability zones. You would typically add more resources like route tables and internet gateways for a complete setup.