Search code examples
amazon-web-servicesamazon-ecsaws-auto-scaling

AWS ECS doesn't start an EC2 Instance when AutoScaling Group has desiredCapacity as 0


Scenario 1:

I have created the following resources in the order:

  • Create AutoScalingGroup/ASG (with minCapacity 1)
  • Create ECS Cluster
  • Attach AutoScalingGroup as Provider to ECS

The problem I face in this case is, an EC2 gets launched by ASG and that doesn't connect to ECS because ECS_CLUSTER value is different from the actual cluster name. This feels obvious because ASG is launched before provider is added to cluster.

Scenario 2:

  • Create AutoScalingGroup/ASG (with minCapacity 0, desiredCapacity 0)
  • Create ECS Cluster
  • Attach AutoScalingGroup as Provider to ECS
  • Add a Task Def and create a Service

The service cannot launch because of an error: "No Container Instances were found in your cluster"

Scenario 2 confuses me - It isn't clear if ECS Scaling Policy would create the very first instance too or not?


Some solutions propose to keep the min capacity as 1, so that it works. But if I do that, I end up into Scenario 1, where the first EC2 instance can't register with ECS.


Solution

  • You have to use user_data in your Launch Configuration or Template to correctly setup the instances for ECS. This includes providing a claster name as explained in AWS docs:

    #!/bin/bash
    cat <<'EOF' >> /etc/ecs/ecs.config
    ECS_CLUSTER=your_cluster_name
    EOF