Format AWS VPC Terraform Configuration
Format a complete AWS VPC configuration with subnets, route tables, internet gateway, and NAT gateway. Demonstrates nested resource formatting and tag alignment.
Detailed Explanation
Formatting AWS VPC Resources
A VPC configuration is one of the most common Terraform patterns, typically involving multiple interconnected resources. Proper formatting makes the network topology visible in the code itself.
Example VPC Configuration
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
instance_tenancy = "default"
tags = {
Name = "main-vpc"
Environment = "production"
ManagedBy = "terraform"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
resource "aws_subnet" "public" {
count = 3
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = {
Name = "public-subnet-${count.index}"
Type = "public"
}
}
Formatting Highlights
- Tag blocks aligned: Within each
tagsblock, keys and values line up vertically - Boolean alignment:
enable_dns_supportandenable_dns_hostnameshave their=andtruevalues aligned - Resource separation: Blank lines between resources and between attribute groups within resources
- Count meta-argument: Placed at the top of the resource block, separated from regular attributes
Network Topology Readability
When your VPC configuration includes 10+ resources (VPC, subnets, route tables, NAT gateways, security groups), consistent formatting lets you scan the file and understand the topology without reading every line.
Use Case
Building production-ready AWS network infrastructure with Terraform, where properly formatted code helps network engineers review CIDR allocations, routing rules, and security group configurations.