Conditionally settings tags with IF not working in cloudformation template

Viewed 326

I want to conditionally create a tag for a resource, not just change the value. The tag should or should not be created based on a condition.

I looked at the docs and from what I found this should work:

  Tags:
    - Key: "sometag"
      Value: "sdfsdfsdfsdf"
    - Key: "someOtherTag"
      Value: "sdfsdfsdfsdf"
    # Only apply this tag if InDevEnvironment is true
    - !If
      - InDevEnvironment
      -
        - Key: "shouldNotEvenExistIfInProd"
          Value: "sdfsdfsdfsdfsdfsdfdsf"
        - !Ref "AWS::NoValue

I get an error: Value of property Tags must be a list of objects

Cloudformation syntax has never been very intuitive to me, do I have the hyphen wrong or something?

1 Answers

I think the reason is that it does not work is due to indentation issues and non-needed double list use. I modified the code below:

Tags:
    - Key: "sometag"
      Value: "sdfsdfsdfsdf"
    - Key: "someOtherTag"
      Value: "sdfsdfsdfsdf"
    - !If
        - InDevEnvironment
        - Key: "shouldNotEvenExistIfInProd"
          Value: "sdfsdfsdfsdfsdfsdfdsf"
        - !Ref "AWS::NoValue
Related