Containerized Microservices#

A production-grade container orchestration setup using ECS Fargate, ALB, ECR, and CI/CD — perfect for microservices architectures with multiple teams and independent deployments.

Architecture Overview#

                    ┌─────────────────────────────────────┐
                    │         Route53 (DNS)                │
                    └────────────────┬────────────────────┘
                    ┌────────────────▼────────────────────┐
                    │        CloudFront + WAF              │
                    └────────────────┬────────────────────┘
                    ┌────────────────▼────────────────────┐
                    │          ALB (HTTPS)                 │
                    │     Path-based routing (/api/*)      │
                    └──┬────────────┬────────────┬─────────┘
                       │            │            │
              ┌────────▼────┐ ┌────▼────┐ ┌────▼────────┐
              │ ECS Service │ │ ECS     │ │ ECS Service │
              │ (Orders)    │ │ Service │ │ (Inventory) │
              │ tasks: 2-6  │ │ (Users) │ │ tasks: 2-4  │
              │ 512 CPU     │ │ tasks:  │ │ 256 CPU     │
              │ 1024MB RAM  │ │ 2-4     │ │ 512MB RAM   │
              └──────┬──────┘ └────┬────┘ └──────┬──────┘
                     │             │              │
              ┌──────▼──────┐     │     ┌────────▼──────┐
              │ SQS (orders)│     │     │ RDS Aurora    │
              └─────────────┘     │     │ (shared)      │
                                  │     └───────────────┘
                          ┌───────▼───────┐
                          │ ElastiCache   │
                          │ (Redis)       │
                          └───────────────┘

Services Used#

Service Purpose Configuration
ECS Fargate Container orchestration Serverless (no EC2 to manage), platform version 1.4+
ECR Container image registry Scan on push, lifecycle policies, immutable tags
ALB Load balancing Path-based routing to different ECS services
RDS Aurora Relational database Serverless v2 (auto-scales), Multi-AZ
ElastiCache Redis Distributed caching Cluster mode disabled, 1 replica
SQS Async communication Standard queues for decoupling services
CodePipeline + CodeBuild CI/CD Build → Test → Push → Deploy (blue/green)
CloudWatch Monitoring & logs Service metrics, container insights, log aggregation

Key Design Decisions#

Decision Rationale
Fargate over EC2 launch type No cluster management, pay per task, auto-scaling built-in
Path-based routing Single ALB routes to multiple services based on URL path
Aurora Serverless v2 Database auto-scales to zero when not in use (cost savings)
Blue/Green deployment Zero-downtime deploys via CodeDeploy with traffic shifting
Sidecar containers App + log agent + metric agent in same task definition
Immutable ECR tags Each build gets a unique tag — easy rollback to any version

Task Definition Example#

{"family": "orders-service",
  "taskRoleArn": "arn:aws:iam::...:role/ecsTaskRole",
  "executionRoleArn": "arn:aws:iam::...:role/ecsExecutionRole",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "512",
  "memory": "1024", "containerDefinitions": [ { "name": "orders-api", "image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/orders:latest", "portMappings": [{ "containerPort": 3000, "protocol": "tcp" }],
      "environment": [
        { "name": "DB_HOST", "value": "aurora-cluster.cluster-xxx.us-east-1.rds.amazonaws.com" },
        { "name": "REDIS_HOST", "value": "redis-cluster.xxx.0001.use1.cache.amazonaws.com" }
      ],
      "logConfiguration": {"logDriver": "awslogs", "options": { "awslogs-group": "/ecs/orders-service", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" }
      }
    }
  ]
}

Real-World Use Case#

Scenario: An e-commerce platform with 3 teams (Orders, Users, Inventory) working independently.

How this architecture handles it:

  • Independent deploys: Each team deploys their service without affecting others
  • Auto scaling: Orders service scales to 50 tasks during Black Friday, Users stays at 4
  • Cost efficiency: Fargate only charges for running tasks — idle services cost nothing
  • Blue/Green: Deploy a new version, test with 10% traffic, then shift 100%
  • Service discovery: ALB routes /orders/* to Orders service, /users/* to Users service

Cost Estimate (Monthly)#

Service Estimated Cost
ECS Fargate (avg 8 tasks) ~$200
ALB ~$22
ECR storage ~$5
RDS Aurora Serverless ~$100
ElastiCache ~$30
CodePipeline ~$15
Total ~$372/month

✅ Key Exam Takeaways#

  • Fargate = serverless containers (no EC2 to manage)
  • ECS vs EKS: ECS is simpler (AWS-native), EKS is Kubernetes (portable)
  • Task definitions define containers, resources, networking, and IAM
  • Service Auto Scaling uses target tracking (CPU, memory, ALB request count)
  • Blue/Green deployments with CodeDeploy = zero-downtime updates
  • Sidecar pattern = app + CloudWatch agent in the same task

📚 References#