gpt4 book ai didi

amazon-web-services - 将标签作为参数传递到cloudformation中

转载 作者:行者123 更新时间:2023-12-03 07:16:29 25 4
gpt4 key购买 nike

我创建了一个简单的模板,将用它来创建 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 个标签。

最佳答案

At the most i will have 5 tags

那么你需要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"

关于amazon-web-services - 将标签作为参数传递到cloudformation中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70424483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com