This guide walks you through deploying a containerized app to AWS ECS using Terraform. It includes:
- VPC with 2 public and 2 private subnets
- Application Load Balancer (ALB)
- ECS Fargate to host a Docker container (running in private subnets)
- Terraform remote state setup
.
βββ app/ # Flask app + Dockerfile
βββ terraform/ # Terraform files
βββ main.tf # Entry point - root module
βββ variables.tf # Input variables
βββ providers.tf # Provider declaration
βββ outputs.tf # Outputs from root
βββ modules/ # Module-based infrastructure
βββ vpc/ # VPC module
βββ alb/ # Load Balancer module
βββ ecs/ # ECS + Task + Service
aws ecr create-repository --repository-name simple-time-service --region ap-south-1
Replace <your_aws_account_id>
with your actual AWS account number:
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin <your_aws_account_id>.dkr.ecr.ap-south-1.amazonaws.com
docker build -t simple-time-service ./app
docker tag simple-time-service:latest <your_aws_account_id>.dkr.ecr.ap-south-1.amazonaws.com/simple-time-service:latest
docker push <your_aws_account_id>.dkr.ecr.ap-south-1.amazonaws.com/simple-time-service:latest
cd terraform
terraform init
# Replace with the correct image URI from Step 3
terraform plan -var="app_image=<your_aws_account_id>.dkr.ecr.ap-south-1.amazonaws.com/simple-time-service:latest"
terraform apply -var="app_image=<your_aws_account_id>.dkr.ecr.ap-south-1.amazonaws.com/simple-time-service:latest"
Once deployed, find the Load Balancer DNS name output from Terraform and visit:
http://<alb_dns>
You should see JSON output with timestamp and IP.
β You're done!