This Terraform project provisions a AWS VPC in two Availability Zones (AZs) for fault tolerance and high availability.
It creates a Multi-AZ VPC with public/private subnets, per-AZ NAT Gateways, VPC Flow Logs, and deploys an ECS Fargate service connected to a private ECR repository behind an Application Load Balancer (ALB).
- CIDR: e.g.,
10.0.0.0/16 - DNS hostnames and DNS support enabled
- Isolated, dedicated network for workloads
-
Public Subnets (x2):
- One per AZ (e.g.,
10.0.0.0/20,10.0.16.0/20) - Host ALB, NAT Gateways
- Auto-assign public IPs
- One per AZ (e.g.,
-
Private Subnets (x2):
- One per AZ (e.g.,
10.0.32.0/20,10.0.48.0/20) - Host ECS tasks, EKS nodes, or databases
- No direct internet access
- One per AZ (e.g.,
- Attached to the VPC
- Enables outbound access for public subnets
- Used for inbound ALB or bastion connectivity
- One NAT Gateway per AZ for fault tolerance
- Private subnets route outbound traffic to their local NAT
- Ensures resiliency during single-AZ failure
- Public Route Table: default route → Internet Gateway
- Private Route Tables (per AZ): default route → NAT Gateway
- Gateway Endpoints: for S3 — keep traffic inside AWS backbone
- Interface Endpoints: for SSM, EC2, CloudWatch, ECR — secure private API access
- Captures ACCEPT / REJECT / ALL traffic metadata
- Sent to CloudWatch Log Group:
/vpc/<project>/flow-logs - IAM Role with least privilege for logging
- Enables audit, security, and performance analysis
- Cluster with container insights enabled
- Task definitions define containers, CPU/memory, and environment variables
- Pulls Docker image from private ECR
- Runs in private subnets (no public IP)
- Logs sent to CloudWatch Logs
- Deployed in public subnets
- Routes inbound traffic to ECS tasks in private subnets
- Supports HTTP and optional HTTPS via ACM certificate
- Health checks and circuit breakers for resilience
- Network Isolation Private workloads only reachable via ALB. No public IPs on ECS tasks
- Per-AZ NAT Gateways AZ-specific egress preventing cross-AZ dependency
- Security Groups ALB SG ingress from trusted CIDRs only. Tasks SG only allows ALB ingress
- IAM Roles Separate task & execution roles. Principle of least privilege enforced |
- Logging & Audit VPC Flow Logs and CloudWatch |
- ECR Hygiene Private repo
- Observability CloudWatch metrics & logs. Supports alerts and dashboards
- Container Image
-
aws ecr get-login-password --region
| docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com -
docker build -t .
-
docker tag :latest .dkr.ecr..amazonaws.com/:v1
-
docker push .dkr.ecr..amazonaws.com/:v1
-
Update container image name in terraform.tfvars file wiht the newly built container and save file.
- Initialize Terraform Run the following commands to initialize and deploy VPC and ECS services:
- terraform init
- terraform plan -out tf.plan
- terraform apply tf.plan
- terraform output alb_dns_name for application dns name