简体   繁体   English

将标签动态应用到 AWS CloudFormation 堆栈

[英]Dynamically apply Tags to AWS CloudFormation Stacks

I have been searching for a way to dynamically applied tags to AWS CloudFormation Stack resource.我一直在寻找一种将标签动态应用到 AWS CloudFormation Stack 资源的方法。 I could not find the right answer.我找不到正确的答案。 Below is my CloudFormation stack resource.下面是我的 CloudFormation 堆栈资源。

AWSTemplateFormatVersion: '2010-09-09' 
Parameters: 
 ami:
  Type: String
 instanceType:
  Type: String
 blockdevice:
  Type: String
Resources: 
 ec2Stack: 
  Type: AWS::CloudFormation::Stack 
  Properties: 
   TemplateURL: !Sub "${s3ArtifactPath}/ec2/ec2-base-app.yml"
   Parameters:
     ami: !Ref ami
     instanceType: !Ref instanceType
     blockdevice: !Ref blockdevice
   Tags:
     - Key: sampleKey
       Value: sampleValue

I have been trying to find a way where I only pass a list or simply ref a parameter is Tags section.我一直在试图找到一种方法,我只传递一个列表或简单地引用一个参数是标签部分。 Here is what I did这是我所做的

AWSTemplateFormatVersion: '2010-09-09' 
Parameters: 
 ami:
   Type: String
 instanceType:
   Type: String
   Default: nbs
   MinLength: 3
   MaxLength: 5
 blockdevice:
   Type: String
 tagsJsonList:
   Type: String
   Default: '[{"key1":"value1"}, {"key2":"value2"}]'
Resources: 
 ec2Stack: 
     Type: AWS::CloudFormation::Stack 
     Properties: 
       TemplateURL: !Sub "${s3ArtifactPath}/ec2/ec2-base-app.yml"
       Parameters:
         ami: !Ref ami
         instanceType: !Ref instanceType
         blockdevice: !Ref blockdevice
       Tags: !Ref tagsJsonList

I was expecting the CloudFormation to formulate the tag list from the variable.我期待 CloudFormation 从变量中制定标签列表。 The purpose behind this will be that we'll be able to scale in the list and the new tags key-value pairs will be added automatically.这背后的目的是我们将能够在列表中进行扩展,并且新的标签键值对将自动添加。 This approach did not work.这种方法不起作用。

I also tried the following way我也尝试了以下方式

AWSTemplateFormatVersion: '2010-09-09' 
Parameters: 
  ami:
    Type: String
  instanceType:
    Type: String
    Default: nbs
    MinLength: 3
    MaxLength: 5
  blockdevice:
    Type: String
Resources: 
  ec2Stack: 
      Type: AWS::CloudFormation::Stack 
      Properties: 
        TemplateURL: !Sub "${s3ArtifactPath}/ec2/ec2-base-app.yml"
        Parameters:
          ami: !Ref ami
          instanceType: !Ref instanceType
          blockdevice: !Ref blockdevice
        Tags:
          - Key: !Select [0, !Split ["=", !Select [0, !Split [",", !ImportValue someExport] ] ] ]
            Value: !Select [1, !Split ["=", !Select [0, !Split [",", !ImportValue someExport] ] ] ]

# someExport -->  k1=v1,k2=v2,k3=v3

  1. One problem with this approach is that I can't use Sub intrinsic function to make the someExport variable dynamic.这种方法的一个问题是我不能使用 Sub 内在 function 来使 someExport 变量动态化。
  2. Other problem is that I'll have to write multiple statements for Key, Value statements for applying multiple tags另一个问题是我必须为键编写多个语句,应用多个标签的值语句

I know you got it working already, but you could try the tag string as:我知道你已经让它工作了,但你可以试试标签字符串:

'{"key1":"value1", "key2":"value2"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM