Using troposphere for cloud formation, how do I add "propagate at launch" to tags

---- UPDATE ---

The feature has been added to the master branch, I just leave my previous answer for reference and in case you don't have access to the latest version of troposphere (ie if you don't clone the repository). You can still use the short function in your code (3rd option), it will work nonetheless.

The "Tags" help class (from troposphere module) cannot generate ASG tags lists (key / value / propagate), only basic tags lists (key / value - for EC2 for example). You can use the troposphere.autoscaling.Tags class instead, which mimics the latest, with the addition of the "propagate" property.

You can use it like this :

    asg.Tags = autoscaling.Tags(Name = 'MyTagName', Version = '123456')

All your tags will have the PropagateAtLaunch property set to 'true'. If you want a different PropagateAtLaunch property, just write like this:

    asg.Tags = autoscaling.Tags(Name = 'MyTagName', Version = '123456', 
      NonPropagatedTag=('fail',False))

The NonPropagatedTag tag will not be propagated (surprise!) and have a value of 'fail'.


Previous answer :

You cannot use the "Tags" helper class (from troposphere module) to generate ASG tags lists (key/value/propagate), only basic tags lists (key/value). A quick look at source code will show you why (https://github.com/cloudtools/troposphere/blob/master/troposphere/init.py)

It leaves you with three options:

  • the long & hard way: an ASG tag list (in troposphere) is just a python list of dicts with three keys : Name, Value and PropagateAtLaunch. So your code will look like :

    asg.Tags= [{'Key':'Name','Value':'MyTagName','PropagateAtLaunch':'true'}, 
      {'Key':'Version','Value':'123456','PropagateAtLaunch':'true'}]
    

    yes, ugly.

  • just a bit shorter: instead of dicts, you can use the autoscaling.Tag helper class, which takes 3 parameters : tag key, tag value, propagate. You will have to code:

    asg.Tags= [autoscaling.Tag('Name','MyTagName','true'),
      autoscaling.Tag('Version','123456','true')]
    

    if you don't have many tags, or just use it in one place, that's OK. But the Tags helper class is so nice...

  • use another helper class, to generate ASG specific tag list. I've just done a pull request on troposphere github repository for this little addition :

    class TagsASG(troposphere.AWSHelperFn):
        defaultPropagateAtLaunch=True
        manyType=[type([]), type(())]
    
        def __init__(self, **kwargs):
            self.tags = []
            for k, v in sorted(kwargs.iteritems()):
                if type(v) in self.manyType:
                  propagate=str(v[1]).lower()
                  v=v[0]
                else:
                  propagate=str(self.defaultPropagateAtLaunch).lower()
                self.tags.append({
                    'Key': k,
                    'Value': v,
                    'PropagateAtLaunch':propagate,
                })
    
        def JSONrepr(self):
            return self.tags
    

Now, you can use it like this:

    asg.Tags = TagsASG(Name = 'MyTagName', Version = '123456')

All your tags will have the PropagateAtLaunch property set to 'true'. If you want a different PropagateAtLaunch property, just write like this:

    asg.Tags = TagsASG(Name = 'MyTagName', Version = '123456', 
      NonPropagatedTag=('fail',False))

The NonPropagatedTag tag will not be propagated (surprise!) and have a value of 'fail'.