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

纵饮孤独 提交于 2019-12-20 12:09:02

问题


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:


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"
            }
       ]
    }
}



回答2:


In the template anatomy, you cant 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"
      }
    }
  }
}


来源:https://stackoverflow.com/questions/27513145/aws-cloud-formation-template-providing-tags-for-the-stack-in-the-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!