Tuesday, July 29, 2025

ECS | Service Auto Scaling with High Performance & Cost Efficiency.

 

ECS Service Auto Scaling automatically adjusts the desired count of tasks in your ECS service based on CloudWatch alarms.

This helps maintain performance and cost-efficiency, especially for variable workloads.

 How ECS Service Auto Scaling Works:

·        ECS Service runs tasks using a task definition.

·        Auto Scaling changes the desired task count (not CPU/memory of the container).

·        CloudWatch Alarms monitor metrics like CPU or memory usage.

·        Scaling Policies define when and how to scale-in or Scale-out.

Prerequisites:

·        ECS cluster and service ( with severless-Fargate or EC2-backed).

·        IAM role with ecs:UpdateService, cloudwatch:*, and application-autoscaling:*.

·        Metrics like CPU Utilization or custom CloudWatch metrics Minitored.

 Key Concepts:

Term

Description

Target Tracking.

Scales to maintain a target metric (e.g., 50% CPU). Most common.

Step Scaling.

Scales based on thresholds and step adjustments.

Scheduled Scaling.

Scales at specific times (e.g., nightly).

 How to Set It Up (CLI or Console)

1. Register the Service with Application Auto Scaling.

# bash
 aws application-autoscaling register-scalable-target \
  --service-namespace twtech-ecs-ns \
  --resource-id service/twtech-ecs-cluster/twtechsprings-ecs-service \
  --scalable-dimension ecs:service:DesiredCount \
  --min-capacity 2 \
  --max-capacity 10

2. Create Scaling Policy

# bash
aws application-autoscaling put-scaling-policy \
  --service-namespace twteh-ecs-ns \
  --resource-id service/twtech-ecs-cluster/twtechspringapp-ecs-service \
  --scalable-dimension ecs:service:DesiredCount \
  --policy-name cpu-target-tracking \
  --policy-type TargetTrackingScaling \
  --target-tracking-scaling-policy-configuration file://policy.json

policy.json example:

# json
{
  "TargetValue": 50.0,
  "PredefinedMetricSpecification": {
    "PredefinedMetricType": "ECSServiceAverageCPUUtilization"
  },
  "ScaleInCooldown": 60,
  "ScaleOutCooldown": 60
}

 Monitoring:

·        Use CloudWatch dashboards to visualize CPU, memory, and task count.

·        Use ECS console or CLI (describe-services) to see desired vs. running task counts.

 twtech Notes:

·        Auto Scaling only adjusts the desired count—your task definition’s CPU/memory don’t change.

·        Works with both Fargate and EC2 launch types.

·        Scaling down to 0 is allowed but be careful with cold starts.

No comments:

Post a Comment

Kubernetes Clusters | Upstream Vs Downstream.

  The terms "upstream" and "downstream" in the context of Kubernetes clusters often refer to the direction of code fl...