Files
Triple-C/aws/terraform/environment/network.tf
T
shadowdaoandClaude Opus 5.5 6911b9de27 fix(aws): final review round for the environment stacks
Critical
- Owner key is sha256("<region>:<aws:userid>") (12 hex): IAM role names are
  account-global and environments regional, so the same user and env name
  in two regions no longer collide. Every caller, the onboarding module's
  owner_key validation, docs and a two-region test.

Important
- A failed onboarding is Incomplete, not Joined: Joined needs the task role
  and, where the bucket is readable, the state's new onboarding_complete
  output (depends_on every resource); a failed or errored local run or a
  stale lock is Incomplete. Join stays as the retry; Follow, Unlock (own
  key only, stale, conditional delete) and Push saved state work on the
  caller's own onboarding.
- errored.tfstate: the runner logs an Error line naming it, follow's
  summary says so, materialize refuses while it exists, and the new
  push_aws_saved_state command runs `tofu state push errored.tfstate`
  through the same runner (file set aside once pushed); UI on the row.
- Bundle README: the bucket is created with the component tag in
  --create-bucket-configuration (the policy's RequestTag condition), plus
  the TLS-only bucket policy; the boundary bundle names an account admin
  WITHOUT the environment-admin files.
- Destroy-incomplete refuses while any users/<env>/ state remains.
- existing network: data.aws_subnet lookups and a precondition (same VPC,
  different AZs) on the task security group.

Minor
- The TLS statement is added to an existing bucket policy, never put over it.
- Deploy progress is emitted to the main window only.
- materialize checks and writes under start.lock.
- UI: managed-elsewhere label and explanation, Incomplete explained,
  Repair/Destroy disabled under a held lock ("Unlock first").
- Cargo.toml libc comment; README: Cancel unavailable on Windows, session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:01:32 -08:00

146 lines
4.4 KiB
Terraform

# The network, by network_mode:
# public new VPC, two public subnets, internet gateway; tasks get a
# public IP (the security group still admits nothing).
# private-nat the same plus two private subnets behind one NAT gateway;
# tasks run in the private subnets without a public IP.
# existing the given vpc_id / subnet_ids; nothing is created here.
locals {
create_vpc = var.network_mode != "existing"
private_nat = var.network_mode == "private-nat"
vpc_cidr = "10.42.0.0/16"
vpc_id = local.create_vpc ? one(aws_vpc.this[*].id) : var.vpc_id
task_subnet_ids = (
var.network_mode == "existing" ? var.subnet_ids
: local.private_nat ? aws_subnet.private[*].id
: aws_subnet.public[*].id
)
assign_public_ip = (
var.network_mode == "existing" ? var.assign_public_ip
: var.network_mode == "public"
)
}
# An existing network is the user's, taken by id: look the subnets up so the
# task security group's precondition (main.tf) can refuse subnets outside
# var.vpc_id or both in one availability zone before anything is built --
# the same check the onboarding module makes of the record.
data "aws_subnet" "existing" {
count = var.network_mode == "existing" ? 2 : 0
id = var.subnet_ids[count.index]
}
# The subnets' AZs must not move once chosen: a changed availability_zone
# replaces the subnet (and the ENIs, NAT gateway, associations and record
# hanging off it). So the list is not filtered on `state` -- a temporary
# impairment would reorder it -- only on opt-in status, which a region's
# default AZs never change; and each subnet ignores later AZ changes.
data "aws_availability_zones" "available" {
count = local.create_vpc ? 1 : 0
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "this" {
count = local.create_vpc ? 1 : 0
cidr_block = local.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = { Name = local.name }
}
resource "aws_internet_gateway" "this" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.this[0].id
tags = { Name = local.name }
}
resource "aws_subnet" "public" {
count = local.create_vpc ? 2 : 0
vpc_id = aws_vpc.this[0].id
cidr_block = cidrsubnet(local.vpc_cidr, 8, count.index)
availability_zone = data.aws_availability_zones.available[0].names[count.index]
tags = { Name = "${local.name}-public-${count.index}" }
lifecycle {
ignore_changes = [availability_zone]
}
}
resource "aws_route_table" "public" {
count = local.create_vpc ? 1 : 0
vpc_id = aws_vpc.this[0].id
tags = { Name = "${local.name}-public" }
}
resource "aws_route" "public_internet" {
count = local.create_vpc ? 1 : 0
route_table_id = aws_route_table.public[0].id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this[0].id
}
resource "aws_route_table_association" "public" {
count = local.create_vpc ? 2 : 0
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public[0].id
}
resource "aws_eip" "nat" {
count = local.private_nat ? 1 : 0
domain = "vpc"
tags = { Name = "${local.name}-nat" }
}
resource "aws_nat_gateway" "this" {
count = local.private_nat ? 1 : 0
allocation_id = aws_eip.nat[0].id
subnet_id = aws_subnet.public[0].id
tags = { Name = local.name }
depends_on = [aws_internet_gateway.this]
}
resource "aws_subnet" "private" {
count = local.private_nat ? 2 : 0
vpc_id = aws_vpc.this[0].id
cidr_block = cidrsubnet(local.vpc_cidr, 8, 10 + count.index)
availability_zone = data.aws_availability_zones.available[0].names[count.index]
tags = { Name = "${local.name}-private-${count.index}" }
lifecycle {
ignore_changes = [availability_zone]
}
}
resource "aws_route_table" "private" {
count = local.private_nat ? 1 : 0
vpc_id = aws_vpc.this[0].id
tags = { Name = "${local.name}-private" }
}
resource "aws_route" "private_nat" {
count = local.private_nat ? 1 : 0
route_table_id = aws_route_table.private[0].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.this[0].id
}
resource "aws_route_table_association" "private" {
count = local.private_nat ? 2 : 0
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[0].id
}