Tuesday, July 29, 2025

Auto Scaling EC2 Instances | with EC2 Launch Type.

Auto Scaling ECS Services using the EC2 Launch Type,.. twtech uses two layers of Auto Scaling:

 Layer 1: ECS Service Auto Scaling

Scales the number of ECS tasks running in twtech service.

 Layer 2: EC2 Auto Scaling Group (ASG)

Scales the number of EC2 instances (twtech container hosts) in the ECS cluster.

 How They Work Together

Component

Purpose

ECS Service Auto Scaling.

Adjusts number of tasks (based on CPU, memory, etc.)

EC2 Auto Scaling Group (ASG).

Ensures there are enough EC2 instances to run those tasks

If twtech ECS service wants to scale up but no EC2 instances are available with enough resources, the new tasks won’t be placed

That’s why twtech must scale both the tasks and the EC2 hosts(instances).

 Step-by-Step Setup (EC2 Launch Type)

Step 1: Create an ECS Cluster with EC2 Instances

Use an EC2 Launch Template or Launch Configuration that:

  • Installs the ECS agent
  • Joins the ECS cluster
  • Has a container-friendly AMI (e.g., Amazon Linux 2 ECS-optimized)

Set up an Auto Scaling Group (ASG) for twtech EC2 instances.

# bash

aws autoscaling create-auto-scaling-group \

  --auto-scaling-group-name twtech-ecs-asg \

  --launch-template LaunchTemplateName=twtech-template \

  --min-size 2 \

  --max-size 10 \

  --desired-capacity 2 \

  --vpc-zone-identifier subnet-abc123xxx,subnet-def456xxx

 Step 2: Attach ECS Capacity Provider to Cluster

# bash

aws ecs create-capacity-provider \

  --name twtsch-capacity-provider \

  --auto-scaling-group-provider autoScalingGroupArn=arn:aws:autoscaling:... \

  --managed-scaling status=ENABLED,targetCapacity=80,minimumScalingStepSize=1,maximumScalingStepSize=100 \

  --managed-termination-protection ENABLED

Attach to the ECS Cluster:

# bash

aws ecs put-cluster-capacity-providers \

  --cluster twtech-ecs-cluster \

  --capacity-providers twtech-capacity-provider \

  --default-capacity-provider-strategy capacityProvider=twtech-capacity-provider,weight=1

 Step 3: Set Up ECS Service Auto Scaling

Same as before, register a scalable target:

# bash

aws application-autoscaling register-scalable-target \

  --service-namespace twtech-ecs-ns \

  --resource-id service/twtech-ecs-cluster/twtech-service \

  --scalable-dimension ecs:service:DesiredCount \

  --min-capacity 2 \

  --max-capacity 10

Define target-tracking policy (e.g., target CPU 60%).

 Step 4: Configure EC2 Auto Scaling Policies (Optional)

twtech can configure its ASG to auto scale based on ECS Cluster Reservation or CloudWatch metrics like CPUReservation or MemoryReservation.

Example metric alarm:

# bash

aws cloudwatch put-metric-alarm \

  --alarm-name ecs-high-cpu \

  --metric-name CPUReservation \

  --namespace AWS/ECS \

  --statistic Average \

  --period 300 \

  --threshold 75 \

  --comparison-operator GreaterThanThreshold \

  --dimensions Name=ClusterName,Value=twtech-ecs-cluster \

  --evaluation-periods 2 \

  --alarm-actions arn:aws:autoscaling:...:policy/twtech-scale-out

 Summary

Layer

What It Scales

Tool

ECS Service Auto Scaling.

Tasks.

application-autoscaling

EC2 ASG Auto Scaling.

EC2 Hosts(instances).

autoscaling + cloudwatch

 

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...