【问题标题】:Can I define tags as a parameter in parameters section AWS CloudFormation template我可以在参数部分 AWS CloudFormation 模板中将标签定义为参数吗
【发布时间】:2018-08-15 10:19:48
【问题描述】:

我可以在参数部分 AWS CloudFormation 模板中将标签定义为参数并在每个创建的资源中使用它吗?

我当前的模板:

AWSTemplateFormatVersion: 2010-09-09
Description: "Some information"
Parameters:
  Test1:
    Type: String
    Description: Test1
    Default: t1
  Test2:
    Type: String
    Description: Test2
    Default: t2

...

LBSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
  GroupDescription: "Enable access to app loadbalancer"
  Tags:
    - Key: Name
      Value: !Join
        - ""
        - - !Ref Test1
          - !Ref Test2
    - Key: Test1
      Value: !Ref Test1
    - Key: Test2
      Value: !Ref Test2

我对任何其他资源(AWS::ElasticLoadBalancing::LoadBalancer、AWS::AutoScaling::AutoScalingGroup 等)使用相同的标签,并且看起来像是重复的代码(每次创建新资源时)。我可以制作这样的东西吗?:

AWSTemplateFormatVersion: 2010-09-09
Description: "Some information"
Parameters:
  Test1:
    Type: String
    Description: Test1
    Default: t1
  Test2:
    Type: String
    Description: Test2
    Default: t2
  Tag:
    #define all tags, which I created above 

...

LBSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
  GroupDescription: "Enable access to app loadbalancer"
  Tags: !Ref Tag

...

很遗憾,我在https://docs.aws.amazon.com/ 上没有找到任何相关信息

【问题讨论】:

  • 您或许可以使用include transform 来执行此操作。但我认为这是一个丑陋的解决方案,所以不是答案。

标签: amazon-web-services templates yaml amazon-cloudformation


【解决方案1】:

您可以改为将标签应用到堆栈,它们将传播到资源。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html

所有堆栈级标签(包括自动创建的标签)都会传播到 AWS CloudFormation 支持的资源。目前,标签不会传播到从块储存设备映射创建的 Amazon EBS 卷。

例如:

aws cloudformation create-stack --stack-name my-stack
                                --template-url <template>
                                --parameters ParameterKey=Param1,ParameterValue=Value1
                                --tags Key=Tag1,Value=Value1

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2017-05-25
    • 2015-03-04
    相关资源
    最近更新 更多