February 12, 2016

Autoscaling and ECS

Recently I was using Amazon's Elastic Container service to deploy a new project and struggled to figure out how to associate an autoscaling group with an ECS cluster. ECS has a nice wizard that will setup a VPC, autoscaling group, and ECS cluster all in one shot. However, I had an existing VPC that I wanted my cluster created in and this was not an option at the time I ran the first run wizard.

The wizard was able to associate and autoscaling group with the cluster so I knew it was possible. I looked all over the ECS documentation and all parts of the ECS and autoscaling UI and could not find any way to connect an autoscaling group with a specific ECS cluster. I was able to bring up instances of the ECS AMI, but they kept being associated with the "default" ECS cluster, which is not what I wanted.

The wizard creates a cloudformation template and I looked at this template to discover how the autoscaling group was being associated with the ECS cluster.

The key is in the launch config user data

After I looked at the cloudformation template it was actually really easy. It was just using a small bash script as the user data for the launch config. It just echos the name of the desired ECS cluster into the ECS agent config.

    #!/usr/bin/env bash
    echo "ECS_CLUSTER=your_cluster_name" >> /etc/ecs/ecs.config

Hopefully this information will save someone else some time. All of the available agent configuration options can be found here.

Tags: AWS EC2 ECS DevOps Docker