The problem
ServiceHub needed production-grade reliability and security without a full cloud-native rebuild. It had to stay on a traditional three-tier architecture, stay up through instance and zone failures, scale on its own, and still fit a small dev budget.
Constraints
- No single point of failure, including the network path out to the internet.
- The database and cache must never be reachable from the internet.
- No manual server setup: instances must configure themselves.
- No static AWS keys on instances.
- Terraform state must be safe for a team to share.
Architecture
Public subnets · 2 AZsApplication Load Balanceronly internet-facing component · 80/443
NAT Gateway × 2one per AZ
↓ port 5000 · only from the ALB security group
Private app subnets · 2 AZsAuto Scaling groupEC2 · Flask + Gunicorn · systemd
Launch templateIMDSv2 required · encrypted EBS
Instance roleone S3 bucket + narrow SSM path
↓ 5432 / 6379 · only from the EC2 security group
Private DB subnets · no internet routeRDS PostgreSQL 15Multi-AZ capable · encrypted
ElastiCache Redisreachable only from EC2
Problems I solved along the way
01
A single NAT Gateway would take down both zones
RiskPrivate instances in both AZs routing through one NAT Gateway means one AZ outage cuts outbound traffic everywhere.
FixOne NAT Gateway per AZ, with each private route table pointing at its own zone’s gateway.
02
IP-based rules are too loose and break when things scale
RiskAllowing the app tier by CIDR range would trust anything in that range, and instance IPs change as the ASG scales.
FixSecurity group chaining: each tier only trusts the security group directly above it (ALB → EC2 → RDS/Redis). There are no CIDR ranges anywhere in the chain.
03
An instance can be “running” and still broken
RiskEC2 status checks only confirm the VM is up, not that the app is answering.
Fixhealth_check_type = "ELB": an instance that fails the app’s HTTP health check is replaced automatically, and rolling instance refresh keeps at least 50% healthy during changes.
04
Stolen instance credentials via SSRF
RiskWith IMDSv1, a server-side request forgery bug can read the instance role’s credentials.
FixThe launch template requires IMDSv2 session tokens with a hop limit of 1, which closes that path.
05
Backend setup that couldn’t be safely re-run
RiskState bucket names must be globally unique, and a bootstrap script that fails on re-run gets edited by hand.
FixAn idempotent setup script creates an account-ID-scoped S3 bucket (versioned, encrypted) and a DynamoDB lock table.
In the code
The database only trusts the app tier’s security group, from modules/security-groups:
ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = [aws_security_group.ec2.id] # no CIDR ranges
}
IMDSv2 and health-check-driven replacement, from modules/ec2:
metadata_options {
http_endpoint = "enabled"
http_tokens = "required" # IMDSv2 only
http_put_response_hop_limit = 1
}
health_check_type = "ELB"
health_check_grace_period = 300
instance_refresh {
strategy = "Rolling"
preferences { min_healthy_percentage = 50 }
}
Module layout
Each layer is its own module, and outputs flow from one to the next (network → security → compute). The dev environment wires them together, and a new environment is a new folder of variables.
terraform/
├── modules/
│ ├── vpc/ # subnets, routing, NAT per AZ, flow logs
│ ├── security-groups/ # ALB → EC2 → RDS / Redis chain
│ ├── iam/ # instance role + scoped policies
│ ├── s3/ alb/ rds/ elasticache/
│ └── ec2/ # launch template + Auto Scaling group
└── environments/dev/
├── main.tf backend.tf user_data.sh
└── setup-backend.sh cleanup.sh
What I learned
- Design subnets per tier first: routing decides what can reach what.
- Trust security groups, not IP ranges.
- Health checks should test the app, not the VM.
user_data plus Terraform outputs means zero-touch servers.
- Remote state with locking is the first thing to build for a team.
Next case studySecure file hosting
Read →