AWS ECS Fargate Production Deployment: Service Patterns and Auto-Scaling Guide

AWS ECS Fargate: Serverless Container Deployment

AWS ECS Fargate deployment provides the simplicity of serverless with the flexibility of containers. Unlike Lambda’s execution time limits and packaging constraints, Fargate runs any containerized application with no infrastructure management. Therefore, teams can focus on application logic while AWS handles server provisioning, patching, and scaling of the underlying compute.

ECS Fargate eliminates the need to manage EC2 instances, AMIs, or cluster capacity. Moreover, you only pay for the vCPU and memory your tasks consume, with per-second billing. Consequently, Fargate is ideal for web applications, API servers, background workers, and any workload that runs longer than Lambda’s 15-minute limit or needs more than 10GB of memory.

Task Definition Best Practices

The task definition is the blueprint for your containers — specifying images, resource limits, environment variables, and logging configuration. A well-designed task definition ensures consistent deployments, proper resource allocation, and comprehensive observability. Furthermore, using Secrets Manager for sensitive values keeps credentials out of your task definitions.

{
  "family": "order-service",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "512",
  "memory": "1024",
  "executionRoleArn": "arn:aws:iam::123456789:role/ecsTaskExecutionRole",
  "taskRoleArn": "arn:aws:iam::123456789:role/orderServiceTaskRole",
  "containerDefinitions": [
    {
      "name": "order-service",
      "image": "123456789.dkr.ecr.us-east-1.amazonaws.com/order-service:v1.5.2",
      "essential": true,
      "portMappings": [
        { "containerPort": 8080, "protocol": "tcp" }
      ],
      "environment": [
        { "name": "SPRING_PROFILES_ACTIVE", "value": "production" },
        { "name": "SERVER_PORT", "value": "8080" }
      ],
      "secrets": [
        {
          "name": "DB_PASSWORD",
          "valueFrom": "arn:aws:secretsmanager:us-east-1:123456789:secret:prod/db-password"
        },
        {
          "name": "API_KEY",
          "valueFrom": "arn:aws:ssm:us-east-1:123456789:parameter/prod/api-key"
        }
      ],
      "healthCheck": {
        "command": ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"],
        "interval": 30,
        "timeout": 5,
        "retries": 3,
        "startPeriod": 60
      },
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/order-service",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ]
}
AWS ECS Fargate container infrastructure
ECS Fargate manages the underlying infrastructure while you focus on container configuration

AWS ECS Fargate Deployment: Auto-Scaling Configuration

Fargate services auto-scale based on CloudWatch metrics — CPU utilization, memory usage, request count, or custom metrics. Target tracking policies are the simplest to configure and most effective for most workloads. Additionally, step scaling provides more granular control for bursty traffic patterns.

# CloudFormation: ECS Service with auto-scaling
OrderService:
  Type: AWS::ECS::Service
  Properties:
    Cluster: !Ref ECSCluster
    TaskDefinition: !Ref OrderTaskDefinition
    DesiredCount: 3
    LaunchType: FARGATE
    DeploymentConfiguration:
      MinimumHealthyPercent: 100
      MaximumPercent: 200
      DeploymentCircuitBreaker:
        Enable: true
        Rollback: true
    NetworkConfiguration:
      AwsvpcConfiguration:
        Subnets: !Ref PrivateSubnets
        SecurityGroups: [!Ref ServiceSG]
    LoadBalancers:
      - ContainerName: order-service
        ContainerPort: 8080
        TargetGroupArn: !Ref TargetGroup

# Auto-scaling
ScalableTarget:
  Type: AWS::ApplicationAutoScaling::ScalableTarget
  Properties:
    ServiceNamespace: ecs
    ResourceId: !Sub "service/${ECSCluster}/${OrderService.Name}"
    ScalableDimension: ecs:service:DesiredCount
    MinCapacity: 2
    MaxCapacity: 20

CPUScalingPolicy:
  Type: AWS::ApplicationAutoScaling::ScalingPolicy
  Properties:
    PolicyName: cpu-tracking
    PolicyType: TargetTrackingScaling
    ScalingTargetId: !Ref ScalableTarget
    TargetTrackingScalingPolicyConfiguration:
      TargetValue: 70.0
      PredefinedMetricSpecification:
        PredefinedMetricType: ECSServiceAverageCPUUtilization
      ScaleInCooldown: 300
      ScaleOutCooldown: 60

Blue-Green Deployments with CodeDeploy

ECS integrates with CodeDeploy for blue-green deployments — running the new version alongside the old one, shifting traffic gradually, and automatically rolling back if health checks fail. Furthermore, you can configure traffic shifting patterns from linear (10% every minute) to canary (10% first, then 90% after validation).

Blue-green deployment architecture
CodeDeploy integration enables zero-downtime blue-green deployments on Fargate

Cost Optimization

Right-size your tasks using Container Insights CPU and memory metrics. Additionally, use Fargate Spot for fault-tolerant workloads (up to 70% savings), and schedule non-critical services to scale down during off-hours. See the AWS ECS Fargate documentation for pricing details and optimization strategies.

  • Use Container Insights to identify over-provisioned tasks
  • Fargate Spot for batch processing and dev/staging environments
  • Savings Plans for predictable production workloads (up to 50% off)
  • Scale to zero for non-critical services during nights/weekends
Cloud cost optimization dashboard
Right-sizing tasks and using Spot capacity can reduce Fargate costs by 50-70%

In conclusion, AWS ECS Fargate deployment provides the ideal balance between serverless simplicity and container flexibility. With proper task definitions, auto-scaling policies, and blue-green deployments, you can run production workloads with confidence. Start with a simple service, add auto-scaling, then implement advanced deployment strategies as your requirements grow.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top