pythonaws-cloudformationaws-clitroposphere

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


I'm using the python module troposphere to generate tags in my cloud formation template. The current script generates:

       "Tags": [{"Key":"Name", "Value":"MyTagName"}, 
                {"Key":"Version", "Value":"123456"}]

but I need to generate

       "Tags":[{"Key":"Name", "Value":"MyTagName", "PropagateAtLaunch":"true"},
               {"Key":"Version", "Value":"123456", "PropagateAtLaunch":"true"}]

The portion of the script that applies is:

    asg = autoscaling.AutoScalingGroup("MyASG")
    asg.Tags = Tags(Name = "MyTagName", Version = "123456")
    t.add_resource(asg)

Solution

  • ---- 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:

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