Guidance Needed for Configuring Dynamic Auto Scaling in AWS ECS (EC2 Launch Type)

0

Hi,

I’m new to AWS ECS clusters. I have deployed an application using an ECS cluster with the EC2 launch type, and now I want to explore auto scaling in detail.

I understand there are two types of scaling involved:

ECS Cluster Auto Scaling

EC2 Service Scaling Group

Here is my current setup:

Target Capacity: 100%

Service Target Capacity: 85%

With this configuration, the application successfully scaled out, but did not scale in as expected. I want to achieve the following behavior: Scale-Out Behavior

When the service memory usage reaches 85%, I want:

A new task to be launched.

If needed, a new EC2 instance to be triggered (since the target capacity is already at 100%).

Scale-In Behavior

When the service memory usage stays below 85% for 3 minutes:

The service should scale in by stopping a task first.

Once the capacity returns to 100%, the EC2 instance should also scale in.

Questions:

Can we control which EC2 instances get terminated during scale-in operations? 

Is there a way to keep a buffer EC2 instance ready, so that during scale-out, it can spin up quickly? Ideally, this instance should not incur charges until it becomes active/running.

I would appreciate guidance on how to configure dynamic auto scaling for both ECS tasks and EC2 instances, including best practices and any official documentation or examples you can share.

1 Answer
0

To achieve the desired auto scaling behavior for your ECS cluster with EC2 launch type, you'll need to configure both service auto scaling and cluster auto scaling. Here's how you can set this up:

  1. Service Auto Scaling:

    • Use Application Auto Scaling to manage your ECS service scaling.
    • Set up a target tracking scaling policy based on memory utilization with a target value of 85%.
    • Configure the scale-out and scale-in cooldown periods. For scale-in, set it to 3 minutes to match your requirement.
  2. Cluster Auto Scaling:

    • Create an ECS Auto Scaling group capacity provider.
    • Set the target capacity to 100% as you've already done.
    • The capacity provider will automatically create CloudWatch alarms and a target tracking scaling policy for the EC2 Auto Scaling group.
  3. EC2 Auto Scaling Group Configuration:

    • Use Launch Templates or Launch Configurations to define the EC2 instances.
    • Set appropriate minimum and maximum capacity limits.

To address your specific questions:

  1. Controlling EC2 instance termination during scale-in: You can influence which EC2 instances get terminated by using termination policies. Options include terminating the oldest or newest instances, or instances closest to the next billing hour. However, you can't precisely control which specific instance gets terminated.

  2. Keeping a buffer EC2 instance ready: Unfortunately, there isn't a way to keep a "ready" EC2 instance without incurring charges. However, you can optimize instance launch times by:

    • Using Amazon ECS-optimized AMIs
    • Pre-pulling container images
    • Using instance warm-up periods in your Auto Scaling group

Best practices:

  • Regularly monitor and adjust your scaling policies based on observed patterns.
  • Use the CapacityProviderReservation metric in CloudWatch to track cluster utilization.
  • Set appropriate cooldown periods to prevent rapid fluctuations.
  • Ensure the instanceWarmupPeriod is sufficient for new instances to be ready.

By implementing these configurations, your ECS cluster should scale out when memory usage reaches 85%, launching new tasks and EC2 instances as needed. It will scale in when memory usage stays below 85% for 3 minutes, first by stopping tasks and then terminating EC2 instances when capacity allows.
Sources
Amazon ECS EC2-backed auto scale-out and scale-in | AWS re:Post
Configuring service auto scaling - Amazon Elastic Container Service
Optimize Amazon ECS service auto scaling - Amazon Elastic Container Service
Scaling - Containers on AWS

profile picture
answered 9 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions