Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Does anyone know how to tag an AWS Autoscale spun EC2 instance via boto? I would like all Autoscale EC2 instances to have a prefix of 'as-'.

Thanks

share|improve this question
    
EC2 doesn't autoscale by itself. How are you initiating it? –  jamieb Aug 21 '12 at 5:59
    
The EC2 instances are scaled up or down based on CloudWatch alarms. –  Binary Maze Aug 22 '12 at 2:51

1 Answer 1

I figured this out.

You assign a 'Name' tag to an AWS Autoscale group after it is created. The trick is to set the 'propagate_at_launch' flag to True when passing it to the Autoscale Tag object. This flag when set to True ensures that the tag will be applied to any Autoscale spun EC2 instance after the tag is created. An example follows:

import boto
from boto.ec2.autoscale import Tag

# Make sure your access keys are stored in ~/.boto
conn = boto.connect_autoscale()

# This assumes you have already setup an elastic load balancer
# and a launch configuration
ag = AutoScalingGroup(group_name=group_name,
                      load_balancers=[load_balancer],
                      availability_zones=availability_zones,
                      launch_config=config,
                      min_size=min_size,
                      max_size=max_size)

# Create auto scaling group
conn.create_auto_scaling_group(ag)

# Fetch the autoscale group after it is created
auto_scaling_group = conn.get_all_groups(names=[group_name])[0]

# Create a Tag for the austoscale group
as_tag = Tag(key='Name',
             value = 'as-instance',
             propagate_at_launch=True,
             resource_id=group_name)

# Add the tag to the autoscale group
conn.create_or_update_tags([as_tag])

Voila! Now whenever an EC2 instance is spun from this Autoscale group based on CloudWatch Alarms i.e. scale up based on CPU utilization thresholds or some other metric...that instance will have a name value = 'as-instance'

Cheers

share|improve this answer
1  
For the record, with the most recent version of boto you can also specify tags parameter on creation of the auto scaling group. –  j0nes Oct 23 '13 at 14:57
    
It is important to note that a tag key can be anything, but if you want it to be the "Name" that shows up in the instance list as the official name, then case matters. In other words, "Name" != "name". –  SunSparc Jan 17 at 19:55
    
Another helpful note: Tags can apply to both AutoScaling Groups and Instances. When a tag is added to an ASG, that does not mean that the instances of that group will be tagged. The propagate_at_launch=True property tells the ASG to apply the tag to the instances it launches as well. –  SunSparc Jan 17 at 22:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.