Search code examples
amazon-web-servicesamazon-ec2amazon-ecs

Instances scaling (by ASG) but no new tasks being created


I have created an ECS cluster, and deployed a 2-container task using a Service (during Service create I associate the Application Load Balancer - using target group of type IP - to it), this works fine (I can access my DNS URL and it loads as expected).

I am now trying to use an AutoScalingGroup for automatically scaling instances (cluster autoscaling), based on CPU utilization.

I have created the ASG (desired instance count=1, max=4), but now need to create a dynamic scaling policy, based on CPU utilization.

If average instance CPU usage is over 50%, I want the ASG to add more instances using simple scaling. (and if ave usage falls under 40, scale back in to min desired count again).

I have added (to my ASG) > dynamic scaling policy (type=simple, scale out 1 instance if alarm triggered) Cloudwatch Alarm=using ClusterName, ServiceName, CPUutilization > 50%, based on previous post.

The alarm goes into In Alarm state and instances are being added by the ASG (desired capacity=1, max=4): instances created

However, no new tasks are being launched on the cluster (it remains at 1, and my app remains unresponsive): no new tasks added

My cluster's instances also remains at 1 (It is as if the newly created ones by the ASG are not being registered to the cluster): cluster instance remains at 1

How to get the cluster to recognize (and use) the newly created instances?


Update:

Thanks Mark, this link has no examples of how to set it up using the AWS console UI (the tutorial further down only does it via the cli).

Are you saying that this is the wrong place to be creating the ASG? EC2 > ASG

When I create a cluster without explicitly assigning an capacity provider (and with it ASG), it creates one automatically. I thought that explicitly creating one via the UI (and an ASG and then attaching it to the CP), then assign that during cluster create gives the same thing? (but apparently not)

A simple online tutorial (in the AWS docs ideally) on how to set this all up would be amazing, but doesn't seem to exist(!).


Update 2: I have updated my service to include Service Autoscaling (created step scaling alarm, to add 1 unit (=Task) as CPUUtilization goes over 50%, as per this aws vid).

I hit the URL with some load, and now see the following under My Service > Events tab:

unable to add another task

So it would seem that the existing instance doesn't have enough resources available - isn't the Service AutoScaling supposed to bring in more EC2 instances if the current ones don't have enough resources?


Solution

  • This is wrong and will not work:

    If average instance CPU usage is over 50%, I want the ASG to add more instances using simple scaling. (and if have usage falls under 40, scale back in to min desired count again).

    The EC2 auto-scaling group provides EC2 instances to your ECS cluster, based on how many tasks the ECS cluster needs to run. That's it. The EC2 auto-scaling group should not be directly triggered by any CPU utilization metrics. The EC2 auto-scaling group should ONLY be triggered by the ECS cluster's capacity provider. You have to let the ECS cluster manage the EC2 auto-scaling group, to create or delete EC2 instances as it needs them, in order to deploy the ECS tasks.

    By adding more EC2 instances to your ECS cluster directly, you are just creating empty EC2 instances without any new tasks to deploy on them, because you haven't done anything to trigger the task count in your ECS service to increase.

    You should be using AWS Application Auto-Scaling to trigger the creation of more ECS tasks, based on the CPU utilization metric of your ECS tasks. When more tasks are created than you have room on the EC2 instances for, the ECS cluster will automatically add more EC2 servers to the cluster and then deploy more ECS tasks.