Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CloudFormation conditional tagging

I have a requirement to conditionally tag resources based on user response to the downtime parameter and the value of environment defined in map. This tag is later used by the lambda function to turn off the instances at night.

I tried it like this with no luck -

Conditions -

    "EnvCheck": {
        "Fn::Not": [
            {"Fn::Equals": [{"Ref": "EnvironmentType"}, "prod"]}
        ]
    },
    "EnableDowntimeTag": {
        "Fn::And": [
            {"Fn::Equals": [{"Ref": "CustodianDowntime"}, "true"]},
            {"Condition": "EnvCheck"}
        ]
    }

Tags section

    "Tags": [
                {
                    "Key": "OwnerContact",
                    "PropagateAtLaunch": "true",
                    "Value": {
                        "Ref": "OwnerContact"
                    }
                },
                {
                    "Condition" : "EnableDowntimeTag",
                    "Key": "custodian_downtime",
                    "PropagateAtLaunch": "true",
                    "Value": "Offhours tz=ET"
                }
            ],

Any idea on how to add a conditional tag?

Thanks!

like image 810
Sandeep G Avatar asked Aug 22 '16 13:08

Sandeep G


2 Answers

And if you're using YAML

  Tags:
    - 'Fn::If':
      - MyCondition
      -
        Key: MyKey
        Value: MyValue
      - !Ref AWS::NoValue
like image 115
Daniel Scott Avatar answered Nov 16 '22 02:11

Daniel Scott


I have a similar need and don't want unused tags lying around either. I've gotten it working using the following snippet:

"Tags": [
  ...,
  {
    "Fn::If": [
      "MyCondition",
      {"Key": "MyKey", "Value": "MyValue"},
      {"Ref": "AWS::NoValue"}
    ]
  }
],
like image 35
dave Avatar answered Nov 16 '22 02:11

dave