How to automatically tag EC2 instances launched by Beanstalk's Auto-Scaling ?
At the time of writing, Amazon Beanstalk does not support to tag any new EC2 instances created by Auto-Scaling. This makes it rather inconvenient when you rely on tagging for billing, for instances.
EDIT 14th May 2013 : it appears I’ve found a better and simpler solution. This new solution assumes that your environment is configured with an IAM EC2 instance role that have enough privileges to add EC2 tags.
Now, add a .ebextensions folder at the root of your application, and add a script.config file. Copy-paste the following code:
01_add_tags: command: ec2-create-tags $(ec2-metadata -i | cut -d ' ' -f2) --tag Client=my-client --tag AnotherTag=another-value env: EC2_HOME: /opt/aws/apitools/ec2 EC2_URL: https://ec2.eu-west-1.amazonaws.com JAVA_HOME: /usr/lib/jvm/jre PATH: /bin:/usr/bin:/opt/aws/bin/
Easy, isn’t it ?
I recommend you the previous, newer solution because it’s better. Firstly, the older solution (described below) needs to be repeated each time the auto-scaling group is created. Secondly, it’s better to be able to change tags through a config file, which the older solution does not allow.
As Amazon Beanstalk does nothing more than launching underlying services, it’s rather easy to overcome this problem by using the Auto-Scaling CLI tool (because Auto-Scaling DOES support tag created instances).
To do this, you need to first download the Auto-Scaling CLI tool and configure it according to the tutorial there (if you are a OS X user, you can use Homebrew to install it more easily with the command “brew install aws-as”. Don’t forget to change the region if you are not using the default one (us-east-1). To change it, you need to modify the variable environment value.
Now that we have installed the CLI tools, let’s launch our Beanstalk environment. It will launch a first EC2 instance, without any tag. By default, Beanstalk automatically add some tags to your EC2 instances, as you can see below:
Let’s add a tag for this first instance, for instance “client” = “my-best-client”:
So far so good. The problem is that if our Auto scaling launches new instances based on criterias set for Beanstalk, new instances won’t contain this new tag. This is a problem.
Firstly, we need to find the auto-scaling identifier. In the screenshots above, this is the value for the key “aws:autoscaling:groupName”. Copy-paste this value. Then open the terminal, and type the following command:
as-create-or-update-tags --tag "id=AUTO_SCALING_NAME, t=auto-scaling-group, k=KEY, v=VALUE, p=true"
The AUTO_SCALING_NAME is the value of the “aws:autoscaling:groupeName”, KEY is the… key of the tag you want to add for every instances, and VALUE the value. Don’t forget to set the p to true, otherwise this tag won’t be propagated to any new instances launched by Auto-scaling:
To try this out, let’s force Beanstalk to create a new instance by modifying the environment settings and changing the min instance from 1 to 2:
And magic ! The tag has been added automatically to the created instance:
Now, you may want to remove the tag from being added to each new instances. Just write the following command (of course, if you want it to be removed from the already launched instances, you must do it manually, it won’t propagate to already launched instances):
as-delete-tags --tag "id=AUTO_SCALING_NAME, t=auto-scaling-group, k=KEY"
I hope Amazon will allow to add default tag directly in Beanstalk environment setting but for now, it’s the only solution that we have. Hopefully, it’s not that complicated !