user2237076
user2237076

Reputation: 331

Cloudformation template parameter to pass tags to resource

I've been racking my brain but cant see how it can be done which seems limited, but I would like to be able to have a template parameter that can be used to pass arbitrary tags to a supported resource in my template, for example:

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

 ....

but later on the resource requires something like:

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

  ....

Is there anyway to achieve this goal?

Upvotes: 6

Views: 9102

Answers (3)

user2237076
user2237076

Reputation: 331

I wanted a generic solution that didn't assume number of tags, order of tags or tag names, and after much trial and error I have managed to solve this using the custom resource solution proposed by @laurent-jalbert-simard.

Here is the gist if anyone else might find useful:

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

Upvotes: 5

krisnik
krisnik

Reputation: 1436

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.

Upvotes: 3

Laurent Jalbert Simard
Laurent Jalbert Simard

Reputation: 6329

No, I don't think it can be done directly. However, you can create a simple custom resource that could take the tags you've passed as parameters and apply them to the EC2 instance.

Upvotes: 1

Related Questions