Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you add EC2 instance attributes at launch?

We're using Amazon ECS for our services. We have a cluster called application and within that cluster, we have several services:

- dev_app
- dev_kafka
- dev_zookeeper
- qa_app
- qa_kafka
- qa_zookeeper
- etc.

And the services pull from task definitions that have correlating constraints, i.e., memberOf(attribute:env == qa), memberOf(attribute:role == zookeeper)

We launch our instances via EC2 launch configurations + Autoscaling Groups. This means that our services can't actually auto-scale right now, because instances launch without the appropriate attributes. The only way I know how to add the attributes currently is to wait for the instance to be added to the application cluster, and go in to manually add custom attributes to each instance.

The question: Can I somehow add instance attributes via the launch configuration, or some other means, at launch?

I've found modify-instance-attribute, but this only seems to be valid for existing attributes, not custom attributes. I've also tried put-attributes, but this seems to only be valid for ECS resources (my instance ARN is apparently invalid).

like image 571
MrDuk Avatar asked Jun 27 '17 21:06

MrDuk


Video Answer


2 Answers

Use "user data" in launch configuration.

echo ECS_INSTANCE_ATTRIBUTES={\"mycostomattr\":\"myvalue\"} >> /etc/ecs/ecs.config

See http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html

like image 195
user3364855 Avatar answered Oct 07 '22 13:10

user3364855


If we are using single agent configuration variable like cluster name, then we use below syntax in user data.

#!/bin/bash
echo "ECS_CLUSTER=MyCluster" >> /etc/ecs/ecs.config 

But if we have multiple variable to write to /etc/ecs/ecs.config use the below format.

#!/bin/bash
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=MyCluster
ECS_ENGINE_AUTH_TYPE=docker
ECS_LOGLEVEL=debug
EOF

In cloudformation, instance user-data we have to apply

"#!/bin/bash -xe\n",
"cat <<'EOF' >> /etc/ecs/ecs.config \n",
"ECS_CLUSTER=", { "Ref": "MyCluster" },
"\n",
"ECS_LOGLEVEL=debug\n",
"ECS_ENGINE_AUTH_TYPE=docker\n",
"EOF"
like image 1
Navneet Joshi Avatar answered Oct 07 '22 12:10

Navneet Joshi