【问题标题】:Pass tags as parameters in clouldformation在 cloudformation 中将标签作为参数传递
【发布时间】:2022-01-22 05:27:51
【问题描述】:

我创建了一个简单的模板,我将用它来创建 s3 存储桶。我的模板是这样的。

Parameters:
  Environment:
    Type: String
    Default: prod
    AllowedPattern: '[a-z\-]+'
  BucketName:
    Type: String
    AllowedPattern: '[a-z\-]+'

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties: 
      BucketName: !Sub foo-${Environment}-${BucketName}
      Tags: 
        - Key: key_1
          Value: foo
        - Key: key_2
          Value: foo
    DeletionPolicy: Retain

我想制作一个通用模板,而不是每次创建 s3 存储桶时都创建不同的模板。我的 s3 存储桶之间唯一会有所不同的是我添加到其中的标签数量。一些 S3 存储桶可能有 2 个标签,而另一些可能有更多。我的 s3 存储桶最多有 5 个标签。所以我想知道是否有办法通过参数传递标签,如果我传递 3 个标签,则创建 3 个标签,如果我传递 2 个标签,则创建 2 个标签。

【问题讨论】:

    标签: amazon-web-services amazon-s3 amazon-cloudformation aws-cloudformation-custom-resource


    【解决方案1】:

    我最多有5个标签

    那么你需要5个参数:

      Tag1:
        Type: CommaDelimitedList
        Default: ""
      # ....
      Tag5:
        Type: CommaDelimitedList
        Default: ""
    

    和5个条件:

    Conditions:
      HasTag1:
       !Not [!Equals [!Ref Tag1, ""] ]
      # ...
      HasTag5:
       !Not [!Equals [!Ref Tag5, ""] ]
    

    然后你使用条件来填充你的标签:

          Tags: 
            - !If 
              - HasTag1
              - Key: !Select [0, !Ref Tag1]
                Value: !Select [1, !Ref Tag1]
              - !Ref "AWS::NoValue"
            # ...  
            - !If 
              - HasTag5
              - Key: !Select [0, !Ref Tag5]
                Value: !Select [1, !Ref Tag5]
              - !Ref "AWS::NoValue"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 2019-04-21
      • 2021-05-09
      • 2020-11-08
      • 2023-02-03
      • 2019-02-05
      • 2020-10-12
      相关资源
      最近更新 更多