0
0
AWScloud~10 mins

ECS service auto scaling in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When your app runs on AWS ECS, traffic can change a lot. ECS service auto scaling helps your app add or remove running copies automatically to handle more or less users smoothly.
When your website gets more visitors during the day and fewer at night, and you want to save money by running fewer servers at night.
When your app suddenly gets a lot of users and you want it to stay fast without crashing.
When you want to keep your app healthy by automatically replacing unhealthy copies.
When you want to avoid manual work of changing server numbers based on guesswork.
When you want to meet user demand without paying for too many servers all the time.
Config File - ecs-service-autoscaling.json
ecs-service-autoscaling.json
{
  "Resources": {
    "MyECSCluster": {
      "Type": "AWS::ECS::Cluster",
      "Properties": {
        "ClusterName": "example-ecs-cluster"
      }
    },
    "MyTaskDefinition": {
      "Type": "AWS::ECS::TaskDefinition",
      "Properties": {
        "Family": "example-task",
        "Cpu": "256",
        "Memory": "512",
        "NetworkMode": "awsvpc",
        "RequiresCompatibilities": ["FARGATE"],
        "ContainerDefinitions": [
          {
            "Name": "example-container",
            "Image": "nginx:1.23",
            "PortMappings": [
              {
                "ContainerPort": 80,
                "Protocol": "tcp"
              }
            ]
          }
        ]
      }
    },
    "MyECSService": {
      "Type": "AWS::ECS::Service",
      "Properties": {
        "Cluster": { "Ref": "MyECSCluster" },
        "TaskDefinition": { "Ref": "MyTaskDefinition" },
        "DesiredCount": 1,
        "LaunchType": "FARGATE",
        "NetworkConfiguration": {
          "AwsvpcConfiguration": {
            "AssignPublicIp": "ENABLED",
            "Subnets": ["subnet-0123456789abcdef0"],
            "SecurityGroups": ["sg-0123456789abcdef0"]
          }
        }
      }
    },
    "MyScalableTarget": {
      "Type": "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties": {
        "MaxCapacity": 4,
        "MinCapacity": 1,
        "ResourceId": { "Fn::Join": ["", ["service/", { "Ref": "MyECSCluster" }, "/", { "Fn::GetAtt": ["MyECSService", "Name"] }]] },
        "RoleARN": "arn:aws:iam::123456789012:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService",
        "ScalableDimension": "ecs:service:DesiredCount",
        "ServiceNamespace": "ecs"
      }
    },
    "MyScalingPolicy": {
      "Type": "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties": {
        "PolicyName": "CpuScalingPolicy",
        "PolicyType": "TargetTrackingScaling",
        "ScalingTargetId": { "Ref": "MyScalableTarget" },
        "TargetTrackingScalingPolicyConfiguration": {
          "TargetValue": 50.0,
          "PredefinedMetricSpecification": {
            "PredefinedMetricType": "ECSServiceAverageCPUUtilization"
          },
          "ScaleInCooldown": 60,
          "ScaleOutCooldown": 60
        }
      }
    }
  }
}

This JSON is a CloudFormation template that sets up ECS service auto scaling.

  • MyECSCluster: Creates an ECS cluster to run your app.
  • MyTaskDefinition: Defines the app container with CPU, memory, and image.
  • MyECSService: Runs the app on Fargate with network settings.
  • MyScalableTarget: Tells AWS which ECS service to scale and the min/max copies.
  • MyScalingPolicy: Sets a rule to keep CPU usage around 50% by adding or removing copies.
Commands
This command creates the ECS cluster, service, and auto scaling setup using the CloudFormation template.
Terminal
aws cloudformation deploy --template-file ecs-service-autoscaling.json --stack-name example-ecs-autoscaling --capabilities CAPABILITY_NAMED_IAM
Expected OutputExpected
Waiting for stack create/update to complete... Successfully created/updated stack - example-ecs-autoscaling
--template-file - Specifies the CloudFormation template file to use.
--stack-name - Names the CloudFormation stack.
--capabilities - Allows creation of IAM roles needed for auto scaling.
Checks the status of the ECS service to confirm it is running and ready for auto scaling.
Terminal
aws ecs describe-services --cluster example-ecs-cluster --services example-ecs-autoscaling-MyECSService-XYZ123
Expected OutputExpected
{ "services": [ { "serviceName": "example-ecs-autoscaling-MyECSService-XYZ123", "status": "ACTIVE", "desiredCount": 1, "runningCount": 1 } ] }
--cluster - Specifies the ECS cluster name.
--services - Specifies the ECS service name to describe.
Verifies the scalable target is registered and ready for scaling actions.
Terminal
aws application-autoscaling describe-scalable-targets --service-namespace ecs --resource-id service/example-ecs-cluster/example-ecs-autoscaling-MyECSService-XYZ123 --scalable-dimension ecs:service:DesiredCount
Expected OutputExpected
{ "ScalableTargets": [ { "ServiceNamespace": "ecs", "ResourceId": "service/example-ecs-cluster/example-ecs-autoscaling-MyECSService-XYZ123", "ScalableDimension": "ecs:service:DesiredCount", "MinCapacity": 1, "MaxCapacity": 4, "RoleARN": "arn:aws:iam::123456789012:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService", "CreationTime": "2024-06-01T12:00:00Z" } ] }
--service-namespace - Specifies the AWS service namespace, here ECS.
--resource-id - Identifies the ECS service to scale.
--scalable-dimension - Defines what property to scale, here the desired count of tasks.
Key Concept

If you remember nothing else from this pattern, remember: ECS service auto scaling adjusts the number of running app copies automatically based on CPU usage to keep your app fast and cost-efficient.

Common Mistakes
Not specifying the correct IAM role ARN for auto scaling in the scalable target.
Without the right IAM role, AWS cannot perform scaling actions, so auto scaling won't work.
Use the AWS managed service role ARN for ECS auto scaling or create a proper IAM role with required permissions.
Setting MinCapacity and MaxCapacity to the same value.
This disables scaling because the service cannot scale beyond that fixed number.
Set MinCapacity lower than MaxCapacity to allow scaling up and down.
Not enabling the right metric in the scaling policy (e.g., using CPU when your app is memory-bound).
Scaling triggers won't match actual load, causing poor performance or wasted resources.
Choose the metric that best reflects your app's load, like CPU or memory utilization.
Summary
Use a CloudFormation template to define ECS cluster, service, scalable target, and scaling policy.
Deploy the template with AWS CLI to create the infrastructure and auto scaling setup.
Verify the ECS service and scalable target status with AWS CLI commands to ensure auto scaling is active.