MrDuk
MrDuk

Reputation: 18322

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).

Upvotes: 8

Views: 3646

Answers (3)

Navneet Joshi
Navneet Joshi

Reputation: 166

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"

Upvotes: 1

user3364855
user3364855

Reputation: 191

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

Upvotes: 14

Ali
Ali

Reputation: 434

If you are looking to pass values and perform specific functions of installing packages on to your instance at the launch then you can do so via the user data.When you define the auto scaling configuration you can specify your input on the third step which has user data text input box.

For example, the below bash script is input as user data and installs the web server on the instance.

 #!/bin/bash
 yum update -y
 yum install -y httpd24 php56 mysql55-server php56-mysqlnd
 service httpd start
 chkconfig httpd on
 groupadd www
 usermod -a -G www ec2-user
 chown -R root:www /var/www
 chmod 2775 /var/www
 find /var/www -type d -exec chmod 2775 {} +
 find /var/www -type f -exec chmod 0664 {} +
 echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Upvotes: 0

Related Questions