Terraformterraformawsvpcnetworkingproduction-ready

AWS VPC Module

A reusable Terraform module for creating a production VPC with public/private subnets, NAT gateways, and flow logs across multiple AZs.


title: "AWS VPC Module" description: "A reusable Terraform module for creating a production VPC with public/private subnets, NAT gateways, and flow logs across multiple AZs." category: "Terraform" language: "hcl" tags: ["terraform", "aws", "vpc", "networking", "production-ready"] createdAt: "2025-01-15" updatedAt: "2025-02-05"

Overview

A reusable VPC module that creates a production-grade network topology with public and private subnets across multiple availability zones. Includes NAT gateways for private subnet internet access with options for cost optimization (single NAT) vs. high availability (one per AZ).

Code

module "vpc" {
  source          = "./modules/vpc"
  name            = "production"
  cidr            = "10.0.0.0/16"
  azs             = ["us-east-1a", "us-east-1b", "us-east-1c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
}

Notes

  • single_nat_gateway = true saves cost in non-production (one NAT instead of per-AZ)
  • Public subnets get an Internet Gateway route; private subnets route through NAT
  • Tags with Tier = "public/private" enable tag-based subnet selection in other modules
  • Flow logs (when enabled) capture all VPC network traffic for security analysis
  • CIDR planning: /16 VPC gives 65K IPs; /24 subnets give 251 usable IPs each