Skip to content
hannctl
PDF

case-studies aws-terraform DESIGN.md

AWS multi-region Terraform Design and implementation

A Terraform module set that builds the same infrastructure in 16 AWS regions from one locals map per region.

16 provider aliases, one assume_roleus-west-2us-west-1us-east-2us-east-1sa-east-1ap-south-1ap-southeast-1ap-southeast-2ap-northeast-3ap-northeast-2ap-northeast-1eu-north-1eu-west-1eu-west-2eu-west-3eu-central-1assume_roleSame role_arn in every region16 regions · 24 modules · one locals map per region
Adding a region means one more locals entry and one more module block.
locals map per regionlocals { networks = { seoul = { demo-seoul = { cidr_block = "10.0.0.0/16" subnets = [ ... ] } } }}for_eachVPCcidr_blockSubnetsubnets listSecurity GroupRule map through for_eachEC2 / EKSInstances and node groups per serviceOne more map entry, that many more resources
Keys are stable, so reordering entries recreates nothing.
EKS 1.32Public and private endpoints, allowed ranges onlyingamer7i.xlarge × 5battlem7i.large × 2mergem7i.large × 2r7i.xlarger7i.xlarger7i.xlarger7i.xlarger7i.xlargem7i.largem7i.largem7i.largem7i.largelabels = { "app/role" = service name }
Node labels separate services, so workloads land on their own node group.

A locals map per region

What this code does: Declares the wanted network state per region as a map. The outer key is the region (seoul), the inner key is the VPC name (demo-seoul), and the value holds the VPC CIDR and its subnet list (name, CIDR, availability zone, and whether public IPs are auto-assigned). Adding a region or a VPC is one more entry in this map; the code that creates resources does not change.

# Declare the wanted state per region. Adding a region or a VPC is one more map entry.
locals {
  networks = {
    seoul = {
      demo-seoul = {
        cidr_block = "10.0.0.0/16"
        subnets = [
          { subnet_name = "demo-seoul-a", subnet_cidr = "10.0.1.0/24", ipv6_cidr_block = null, availability_zone = "ap-northeast-2a", autoassign_public = "false", tags = {} },
          { subnet_name = "demo-seoul-c", subnet_cidr = "10.0.2.0/24", ipv6_cidr_block = null, availability_zone = "ap-northeast-2c", autoassign_public = "false", tags = {} },
        ]
      }
    }

A module with for_each reads the map

What this code does: Runs the VPC module once for each entry in the Seoul map (local.networks.seoul). The map key (each.key) becomes the VPC name, and the CIDR and subnet list come from the value (each.value). The providers line binds the Seoul alias (aws.seoul), so everything this block creates lands in Seoul, and because the key is the resource address, reordering the map never recreates an existing VPC.

module "seoul_vpc" {
  source   = "../../modules/vpc"
  for_each = local.networks.seoul

  providers = { aws = aws.seoul }

  vpc_name            = each.key
  cidr_block          = each.value.cidr_block
  instance_tenancy    = "default"
  use_ipv6_cidr_block = false
  subnets             = each.value.subnets
}
© 2026 Kim JunHyun · Built with Astro · The infrastructure behind this site is code too

↑↓ move · Enter open · Esc close

Choose a theme

Click a badge to switch to that theme on the same content. Dragging only swings it.

Back to the front page →