gpt4 book ai didi

amazon-web-services - 将标签动态应用到 AWS CloudFormation 堆栈

转载 作者:行者123 更新时间:2023-12-03 07:43:34 26 4
gpt4 key购买 nike

我一直在寻找一种将标签动态应用到 AWS CloudFormation Stack 资源的方法。我找不到正确的答案。以下是我的 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

我一直在尝试找到一种方法,只传递列表或简单地引用参数是标签部分。这是我所做的

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

我期望 CloudFormation 从变量中制定标签列表。其背后的目的是我们将能够扩展列表,并且新标签键值对将自动添加。这个方法没有奏效。

我也尝试了以下方法

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. 这种方法的一个问题是我无法使用 Sub 内部函数使 someExport 变量动态化。
  2. 另一个问题是我必须为 Key、Value 语句编写多个语句才能应用多个标签

最佳答案

我知道你已经让它工作了,但你可以尝试将标签字符串设置为:

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

关于amazon-web-services - 将标签动态应用到 AWS CloudFormation 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593732/

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