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
| Resource | Description |
|---|---|
| aws_vpc.this | Main VPC with /16 CIDR |
Input Variables
| Name | Type | Default | Description |
|---|---|---|---|
| stage_index | number | 0 | Index for CIDR calculation (0, 1, 2, ...) |
| reserved_offsets | list(number) | [0,1,2,3] | Used offsets (skip 172.30-33) |
| project | string | "MILU2" | Project tag |
| environment | string | "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_index | VPC CIDR | Subnet main | Subnet second |
|---|---|---|---|
| 0 | 172.34.0.0/16 | 172.34.0.0/24 | 172.34.1.0/24 |
| 1 | 172.35.0.0/16 | 172.35.0.0/24 | 172.35.1.0/24 |
| 2 | 172.36.0.0/16 | 172.36.0.0/24 | 172.36.1.0/24 |
Outputs
| Name | Description |
|---|---|
| vpc_id | VPC ID |
| vpc_cidr | VPC CIDR block |
Dependencies
Info
This is the first module, no dependencies on other modules.
Used by
- 02-Subnets - Needs vpc_id to create subnets
- 03-Internet Gateway - Needs vpc_id to attach IGW
- 05-Security Groups - Needs vpc_id to create SGs
- 06-ALB - Needs vpc_id to create ALB