Add EC2 to ECS Cluster
I have had a lot of problems with ecs-agent always connecting to the “default” cluster rather than the one I want it to connect to. All the other instructions missed one vital step – removing ecs-agent from docker and reinstalling – that way it re-reads the ECS config file – without this step I could not get ecs-agent to connect to anything other than “default” cluster.
To put an EC2 ecs-agent into a specific cluster do the following:
Edit the ecs configuration file at /etc/ecs/ecs.config: sudo vi /etc/ecs/ecs.config
Add: ECS_CLUSTER=YourCluster
Close the editor “:wq”
Stop ecs-agent: sudo docker stop ecs-agent
Remove ecs-agent from docker: sudo docker rm ecs-agent
Remove the ecs-agent checkpoint file: sudo rm /var/lib/ecs/data/ecs_agent_data.json
Optional – remove the ECS log files: sudo rm /var/log/ecs/*
Reinstall ecs-agent using the command at https://github.com/aws/amazon-ecs-agent – it was (at writing):
sudo docker run --name ecs-agent \ --detach=true \ --restart=on-failure:10 -d \ --volume=/var/run/docker.sock:/var/run/docker.sock \ --volume=/var/log/ecs:/log \ --volume=/var/lib/ecs/data:/data \ --net=host \ --env-file=/etc/ecs/ecs.config \ --env=ECS_LOGFILE=/log/ecs-agent.log \ --env=ECS_DATADIR=/data/ \ --env=ECS_ENABLE_TASK_IAM_ROLE=true \ --env=ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=true \ amazon/amazon-ecs-agent:latest
Start ecs-agent: sudo docker start ecs-agent
If you are using an ECS Optimized image you will need to add the container auto-start by: sudo vi /etc/init/ecs-agent.conf
Add the lines:
description "ecs-agent container" author "Me" start on filesystem and started docker stop on runlevel [!2345] respawn script /usr/bin/docker start -a ecs-agent end script