0

I have an ECS cluster defined in AWS and an Auto Scaling Group that I use to add/remove instance to handle tasks as necessary. I have the ASG setup so that it is creating the EC2 instance at the appropriate time, but it won't connect to the ECS Cluster unless I manually go in and disable/enable the ECS service.

I am using the Amazon Linux 2 ami on the EC2 machines and everything is in the same region/account etc. I have included my user data below.

#!/bin/bash 
yum update -y 
amazon-linux-extras disable docker 
amazon-linux-extras install -y ecs 
echo "ECS_CLUSTER={CLUSTERNAME}" >> /etc/ecs/ecs.config
systemctl enable --now ecs

As mentioned this installs the ECS service and sets the config file properly but the enable doesn't actually connect the machine, but running the same disable/enable commands on the machine once running connects without problem. What am I missing?

HammNCheez
  • 71
  • 8

2 Answers2

0

First thing, the correct syntax is

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

Once you update the config better to restart the ECS agent.

#!/usr/bin/env bash
echo "ECS_CLUSTER=CLUSTER_NAME" >> /etc/ecs/ecs.config
sudo yum update -y ecs-init
#this will update ECS agent, better when using custom AMI
/usr/bin/docker pull amazon/amazon-ecs-agent:latest
#Restart docker and ECS agent
sudo service docker restart
sudo start ecs
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Thanks for the reply, I do have the ECS_CLUSTER piece in there, just deleted when I removed the actual name. I gave this a try and am getting the same results. The website below is what I initially used to get my start and it looks like the commands you have are for version 1 of amazon linux, might still work overall though. https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-install.html – HammNCheez Jul 07 '20 at 14:25
0

I ended up solving this using the old adage, turn it off and on again.

e.g. I added shutdown -r 0 to the bottom of the user data script to restart the machine after it was "configured" and it connected right now.

HammNCheez
  • 71
  • 8