Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS cloud formation Template- providing Tags for the stack in the template

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.

like image 808
user2716913 Avatar asked Dec 16 '14 20:12

user2716913


People also ask

How do I specify tags in CloudFormation template?

The key name of the tag. You can specify a value that's 1 to 128 Unicode characters in length and can't be prefixed with aws: . You can use any of the following characters: the set of Unicode letters, digits, whitespace, _ , . , / , = , + , and - . The value for the tag.

How do I add tags to an existing CloudFormation stack?

No, you cannot add tags to the existing CloudFormation stack without updating the stack. When you add new Tags to CloudFormation, stack will go to UPDATE_IN_PROGRESS state and updates the wewly added tags to all the supported resources in the stack. Updating/Adding tags will not replace the resources.

What part of a CloudFormation template allows you to pass values into the template?

Parameters (optional) Values to pass to your template at runtime (when you create or update a stack). You can refer to parameters from the Resources and Outputs sections of the template.


3 Answers

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"
      }
    }
  }
}
like image 131
lalyos Avatar answered Oct 21 '22 00:10

lalyos


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:

  • See: create-stack CLI documentation

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"
    }]
  }
}
like image 22
John Rotenstein Avatar answered Oct 21 '22 01:10

John Rotenstein


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

like image 33
Chux Uzoeto Avatar answered Oct 20 '22 23:10

Chux Uzoeto