51

We wanted to use company specific Tags to the resources that we create in AWS for billing purposes. I am using a cloud formation template to spin up our Elasticbeanstalk instance and other project dependent resources. When I use the CloudFormation console to create a stack it asks me for Tags in the page after parameters. I have to manually input the Tags for that stack. However is there a way to specify those Tags (Tags for the stack) with in the cloud formation template itself? That way the Tag gets propagated to the other resources? I know that the cloud formation automatically tags the resources with the stack name. But we need company specific tags to bill separate departments.

1

5 Answers 5

56

In the template anatomy, you can't set stack-level tags directly. However you can create a wrapper template, having a single resource of AWS::CloudFormation::Stack.

You can define stack-level tags on that resource:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "WrapperTemplate",

  "Resources": {
    "WrappedStackWithStackLevelTags": {
      "Type" : "AWS::CloudFormation::Stack",
      "Properties" : {
        "Tags" : [ { "Key" : "Stage", "Value" : "QA" } ],
        "TemplateURL" : "your-original-template-s3-url"
      }
    }
  }
}
7
  • I didn't even know this was an option... this is incredibly helpful for much more than just tag inheritance.
    – VictorKilo
    Feb 16, 2017 at 19:59
  • 1
    Is this how you would set SNS Topic for the entire stack from within the template?
    – Kappacake
    May 4, 2018 at 15:43
  • Man Incredibly useful!! Should update the title to include "cloud formation stack level tags"
    – user6434796
    May 21, 2018 at 17:09
  • 1
    Wouldn't that create two stacks? A parent stack and a nested one
    – lfk
    Feb 13, 2019 at 5:20
  • 4
    Can this be used without setting the templateUrl? I don't want to create a nested stack
    – lvthillo
    Feb 28, 2019 at 20:03
42

When launching AWS CloudFormation, the tags being requested will be applied to the CloudFormation Stack itself and (where possible) will also be propagated to the resources launched by the Stack.

These tags can be passed to the CreateStack API call, or from the CLI:

These tags are applied to the whole Stack and aren't included in the CloudFormation template.

However, CloudFormation templates can include tags for specific resources that are being created. For example, when launching Amazon EC2 instances, tags can be included in the template:

"MyInstance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "SecurityGroups" : [{ "Ref" : "MySecurityGroup" }],
    "AvailabilityZone" : "us-east-1a",
    "ImageId" : "ami-20b65349",
    "Volumes" : [{
      "VolumeId" : { "Ref" : "MyEBS" },
      "Device" : "/dev/sdk"
    }],
    "Tags" : [{
      "Key" : "Stage",
      "Value" : "QA"
    }]
  }
}
2
  • Thanks John. (where possible) is the hint that I needed. Tagging an elastic Beanstalk environment is not supported through cloudformation , however you can tag it Manually. I was assuming if it supported to TAg through console it should be able to be done through cloud formation and I was wrong. Dec 18, 2014 at 19:34
  • 7
    Tag propagation in CloudFormation has been a bit unpredictable in my experience. It's disappointing since the documentation claims, "All stack-level tags, including automatically created tags, are propagated to resources that AWS CloudFormation supports."
    – Shannon
    Sep 10, 2015 at 15:09
13

Contrary to what @lalyos says, you don't need to use nested stacks for this, just provide the tags that should apply to all resources as stack level tags.

These stack-level tags can be specified whether running the stack on the console or via CLI.

CLI example:

aws cloudformation create-stack --stack-name my-stack-name \
 --template-body file://path-to-template-file.yaml \
 --parameters ParameterKey=param1key,ParameterValue=param1value \
 --tags Key=tag1key,Value=tag1value \
        Key=tag2key,Value=tag2value \
        Key=tag3key,Value=tag3value 

... and generally add as many tags as you need, using the same format and allowing spaces between tag key-value pairs

See: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-add-tags.html

1
  • 1
    I use aws cloudformation deploy and this works great! The only difference is that the tags are specified in the format --tags Key1=Value1 Key2=Value2. See here
    – DV82XL
    Jun 10, 2021 at 19:12
4

You can build and deploy a CloudFormation template using aws-sam-cli. This command reads a samconfig.toml file where you can declare tags for all the resources of the stack (including CloudFormation stack itself)

Your samconfig.toml should look like:

[default.deploy.parameters]
stack_name = "your-application"
s3_bucket = "your-s3-for-cloudformation-stuff"
s3_prefix = "your-folder-name"
...
tags = "Stage=\"QA\""

and then run:

sam build --template <your-cloudformation-template.yml> && sam deploy
1
  • quick and simple - should be best answer. Tags can also be chained by just using blanks/empty spaces: tags = "Stage=\"QA\" Project=\"xyz\""
    – snibbo
    Dec 19, 2022 at 9:57
1

You don't need any wrapper.. You can add tags to the stack on when you create/update it:

In Console: Console

You can also use the aws cli:

aws cloudformation create-stack help

   --tags (list)
      Key-value  pairs  to  associate with this stack. CloudFormation also
      propagates these tags to supported resources in the stack.  You  can
      specify a maximum number of 50 tags.

      If  you  don't specify this parameter, CloudFormation doesn't modify
      the stack's tags. If you specify an empty value, CloudFormation  re-
      moves all associated tags.

      (structure)
          The Tag type enables you to specify a key-value pair that can be
          used to store information about an CloudFormation stack.

          Key -> (string)
             Required . A string used to identify this tag. You can  spec-
             ify  a maximum of 128 characters for a tag key. Tags owned by
             Amazon Web Services (Amazon Web Services) have  the  reserved
             prefix: aws: .

          Value -> (string)
             Required  .  A  string containing the value for this tag. You
             can specify a maximum of 256 characters for a tag value.

   Shorthand Syntax:

      Key=string,Value=string ...

   JSON Syntax:

      [
        {
          "Key": "string",
          "Value": "string"
        }
        ...
      ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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