用于将标记传递给资源的Cloudformation模板参数

时间:2018-02-01 14:52:16

标签: amazon-web-services amazon-cloudformation

我一直在绞尽脑汁,但无法看到它是如何做的似乎有限,但我希望能够有一个模板参数,可用于将任意标签传递到我的模板中支持的资源,例如:

EC2Tags:
  Description: Tags to add to the EC2 Instance
  Type: CommaDelimitedList
  Default: "CreatedBy=JohnDoe,Name=NewEC2,OtherTag=OtherValue"

 ....

但稍后资源需要:

Type: "AWS::EC2::Instance"
Properties:
  Tags: 
    - 
     Key: "keyname1"
     Value: "value1"
    - 
     Key: "keyname2"
     Value: "value2" 

  ....

有没有实现这个目标?

3 个答案:

答案 0 :(得分:3)

我想要一个没有假设标签数量,标签顺序或标签名称的通用解决方案,经过多次试验和错误后,我设法使用@ laurent-jalbert提出的自定义资源解决方案解决了这个问题。锡马德。

如果其他人可能觉得有用,那么这就是要点:

https://gist.github.com/ispyinternet/97b434a2a58aea5d496ecd87b29e64e9

答案 1 :(得分:1)

不,我认为不能直接完成。但是,您可以创建一个简单的custom resource,它可以将您传递的标记作为参数并将其应用于EC2实例。

答案 2 :(得分:0)

There is a way of achieving that if the number of tag values that you pass are definite.

There is function called Fn::Select to work with CommaDelimitedValues.

Here is some code snippet that can work.

EC2Tags:
  Description: Tags to add to the EC2 Instance
  Type: CommaDelimitedList
  Default: "JohnDoe,NewEC2,OtherValue"


Type: "AWS::EC2::Instance"
Properties:
  Tags: 
  - 
    Key: "CreatedBy"
    Value: !Select [ 0, !Ref EC2Tags ]
  - 
    Key: "Name"
    Value: !Select [ 1, !Ref EC2Tags ]
  - 
    Key: "OtherTag"
    Value: !Select [ 2, !Ref EC2Tags ]

Hope this helps.