A complete containerized Python application deployed on AWS using Terraform, featuring automated CI/CD, high availability, and production-ready monitoring.
- High Availability: Multi-AZ deployment across 2 availability zones
- Auto Scaling: ECS Fargate with configurable scaling policies
- Security: Network isolation with security groups and private subnets
- Monitoring: Complete CloudWatch integration with logs and metrics
- SSL/TLS: HTTPS support with ACM certificate integration
- Database: Managed PostgreSQL RDS with automated backups
- Secrets Management: AWS Secrets Manager for secure credential storage
- Automated Builds: GitHub Actions workflow for containerization
- Container Registry: Amazon ECR for Docker image storage
- Zero-Downtime Deployments: Rolling updates with health checks
- Environment Separation: Support for dev/staging/prod environments
.
βββ main.tf # Root Terraform configuration
βββ variables.tf # Root variables
βββ modules/
β βββ network/ # VPC, subnets, routing
β β βββ main.tf
β β βββ variables.tf
β βββ security/ # Security groups
β β βββ main.tf
β β βββ variables.tf
β βββ alb/ # Application Load Balancer
β β βββ main.tf
β β βββ variables.tf
β βββ ecr/ # Container registry
β β βββ main.tf
β β βββ variables.tf
β βββ ecs/ # Container orchestration
β β βββ main.tf
β β βββ variables.tf
β βββ rds/ # PostgreSQL database
β β βββ main.tf
β β βββ variables.tf
β βββ monitoring/ # CloudWatch setup
β βββ main.tf
β βββ variables.tf
βββ .github/
β βββ workflows/
β βββ deploy.yml # CI/CD pipeline
βββ app/ # Python application code
β βββ app.py
β βββ requirements.txt
β βββ Dockerfile
βββ README.md
- IaC: Terraform with modular architecture
- Cloud Provider: AWS
- Networking: VPC, Application Load Balancer, NAT Gateway
- Compute: ECS Fargate (serverless containers)
- Database: Amazon RDS PostgreSQL
- Storage: Amazon ECR for container images
- Security: AWS Secrets Manager, Security Groups
- Monitoring: CloudWatch Logs & Metrics
- Runtime: Python 3.x
- Framework: Flask/FastAPI
- Database: PostgreSQL
- Containerization: Docker
- Port: 5000 (configurable)
- CI/CD: GitHub Actions
- State Management: S3 backend with DynamoDB locking
- Environment Management: Terraform workspaces
- ALB Security Group: Allows HTTP (80) and HTTPS (443) from internet
- ECS Security Group: Allows traffic only from ALB on port 5000
- RDS Security Group: Allows PostgreSQL (5432) only from ECS tasks
- ECS Execution Role: ECR image pulling, CloudWatch logging
- ECS Task Role: Secrets Manager access for database credentials
- AWS Account with appropriate permissions
- Terraform >= 1.0
- AWS CLI configured
- Docker for local testing
- GitHub repository with Actions enabled
-
Clone the repository
git clone <repository-url> cd <repository-name>
-
Configure Terraform backend
# Create S3 bucket and DynamoDB table for state management aws s3 mb s3://my-terraform-state-smd29-py aws dynamodb create-table \ --table-name terraform-lock-table \ --attribute-definitions AttributeName=LockID,AttributeType=S \ --key-schema AttributeName=LockID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 -
Initialize Terraform
terraform init
-
Create workspace (optional)
terraform workspace new dev terraform workspace new prod
# Plan deployment
terraform plan
# Apply infrastructure
terraform apply
# Get outputs
terraform output
# Destroy infrastructure
terraform destroyThe GitHub Actions workflow automatically:
- Builds Docker image from your Python application
- Pushes to ECR repository
- Updates ECS service with new image
- Performs health checks and rollback if needed
Set up these GitHub Secrets:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_REGION
- Application Logs: Centralized container logging
- Infrastructure Metrics: ECS, ALB, RDS performance metrics
- Custom Alarms: CPU, memory, database connection monitoring
- Log Retention: Configurable retention periods
- ALB Health Check:
/healthendpoint monitoring - ECS Service Health: Container health and restart policies
- RDS Monitoring: Database performance and connectivity
## π Troubleshooting
### Common Issues
**ECS Tasks Not Starting**
- Check CloudWatch logs: `/aws/ecs/<cluster-name>`
- Verify ECR image exists and is accessible
- Check IAM permissions for task roles
**ALB Health Check Failures**
- Ensure `/health` endpoint returns 200 status
- Verify security group allows traffic on port 5000
- Check ECS task health and logs
**Database Connection Issues**
- Verify RDS security group allows ECS access
- Check Secrets Manager permissions
- Validate database credentials in secrets
**Terraform State Issues**
- Ensure S3 bucket and DynamoDB table exist
- Check AWS credentials and permissions
- Verify backend configuration in `main.tf`
### Useful Commands
```bash
# Check ECS service status
aws ecs describe-services --cluster <cluster-name> --services <service-name>
# View ECS task logs
aws logs get-log-events --log-group-name <log-group> --log-stream-name <stream>
# Test ALB endpoint
curl -I http://<alb-dns-name>/health
# Check RDS connectivity
psql -h <rds-endpoint> -U <username> -d <database>