01-VPC

Module creating VPC with auto CIDR from stage_index

Stage Modulemodules/01-vpc

Purpose

Creates a dedicated VPC for each stage deployment. CIDR is auto-calculated to avoid conflicts when deploying multiple stages in the same account.

Created Resources

ResourceDescription
aws_vpc.thisMain VPC with /16 CIDR

Input Variables

NameTypeDefaultDescription
stage_indexnumber0Index for CIDR calculation (0, 1, 2, ...)
reserved_offsetslist(number)[0,1,2,3]Used offsets (skip 172.30-33)
projectstring"MILU2"Project tag
environmentstring"stage"Environment tag

CIDR Calculation Logic

CIDR is calculated based on stage_index and reserved_offsets:

CIDR Calculation
# locals.tf
locals {
  # reserved_offsets = [0,1,2,3] → skip 172.30, 172.31, 172.32, 172.33
  # stage_index = 0 → 172.34.0.0/16
  # stage_index = 1 → 172.35.0.0/16
  # stage_index = 2 → 172.36.0.0/16
  
  base_cidr   = 30  # Starting point
  offsets     = range(0, 100)  # Generate 0-99
  available   = [for o in local.offsets : o if !contains(var.reserved_offsets, o)]
  actual_offset = local.available[var.stage_index]
  
  vpc_cidr = "172.${local.base_cidr + local.actual_offset}.0.0/16"
}
stage_indexVPC CIDRSubnet mainSubnet second
0172.34.0.0/16172.34.0.0/24172.34.1.0/24
1172.35.0.0/16172.35.0.0/24172.35.1.0/24
2172.36.0.0/16172.36.0.0/24172.36.1.0/24

Outputs

NameDescription
vpc_idVPC ID
vpc_cidrVPC CIDR block

Dependencies

Info

This is the first module, no dependencies on other modules.

Used by