Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse list of CloudFormation tags

I have a rather complex set of CloudFormation templates that I use for provisioning the different environments of our infrastructure. However I recently got the request to tag the created resources with a rather extensive list of tags (like 15).

Hardcoding the tags into each of the template file doesn't seem like a good idea to me. I would rather create the list of tags once and refer to them for every taggeable resource. Problem is: I am not even sure this is possible. Do you know of any way that a reusable list of tags can be achieved?

I want to write this:

ECSAutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
        ...
        Tags: !Ref ElTags

rather than

ECSAutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
        ...
        Tags: !Ref ElTags
            - Key: Name
              Value: !Join ["-", [!Ref EnvironmentName, 'ecs-as-group'] ]
              PropagateAtLaunch: true
            - Key: TEAM
              Value: !Ref TeamName
              PropagateAtLaunch: true
like image 837
Bram Vandewalle Avatar asked Sep 26 '17 11:09

Bram Vandewalle


People also ask

How can I reuse CloudFormation template?

You simply create a separate template for the resources that you want to reuse and then save that template in an Amazon S3 bucket. Whenever you want to add those resources in another template, use the AWS::CloudFormation::Stack resource to specify the S3 URL of the nested template.

How do I update CloudFormation stack tags?

To update a AWS CloudFormation stack (console)In the AWS CloudFormation console , from the list of stacks, select the running stack that you want to update. In the stack details pane, choose Update. If you haven't modified the stack template, select Use current template, and then choose Next.

How do I rollback CloudFormation stack?

If you use nested stacks, rolling back the parent stack will attempt to roll back all the child stacks as well. Open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . Select the stack that you want to update, choose Stack actions, and then choose Continue update rollback.

Can CloudFormation stacks have same name?

Stack names are "reusable". Each stack deployment is identified by its own unique Stack ID . The stack ID lets CloudFormation keep track of the stack history across multiple deployments if the same stack name.


1 Answers

You can easily reuse tags if you don't pass them to the template but instead reference them during deployment. The tags will be propagated to all ressources of the stack. A command similar to the one below might meet your needs:

aws cloudformation create-stack --stack-name mystack \
--template-body file://my_template.yaml --tags file://my_tags.json

The file my_tags.json is of the format

[
    {"Key": "mytag", "Value": "val"},
    ...
]

Alternatively, you could deploy your stack through CodePipeline and define the tags in the template configuration

like image 96
Michael Panchenko Avatar answered Sep 19 '22 07:09

Michael Panchenko