简体   繁体   English

AWS CloudFormation条件标记

[英]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. 我需要根据用户对停机时间参数的响应以及map中定义的环境值来有条件地标记资源。 This tag is later used by the lambda function to turn off the instances at night. 此标记稍后由lambda函数用于在晚上关闭实例。

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! 谢谢!

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"}
    ]
  }
],

Unfortunately I don't think it's possible to conditionally add a tag. 不幸的是,我不认为有条件地添加标签是可能的。 The best you can do is use the condition to modify the data for the tag - either the key or value or both. 您可以做的最好的事情是使用条件来修改标记的数据 - 键或值或两者。 If you modify the key, then any automated searching for the expected tag key will not find resources with the modified key. 如果您修改密钥,则任何自动搜索预期标记密钥都不会找到具有修改密钥的资源。 But you will get left with unnecessary tags lying around. 但是你会留下不必要的标签。

eg 例如

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

With this, if the condition EnableDowntimeTag is true, then a tag with key custodian_downtime is created, and a value of Offhours tz=ET . 借助于此,如果条件EnableDowntimeTag为真,则与键标签custodian_downtime被创建,并且值Offhours tz=ET

If the condition is not true, a tag with key no_custodian_downtime is created, with an empty value. 如果条件不为真,则创建具有键no_custodian_downtime的标记,其值为空。

If you are using the tags to identify resources from an automated process, this should help it to only identify the required resources - while also providing an explicit reference on those with the no_custodian_downtime tag that there is no downtime required - so it could be even more helpful. 如果您使用标签来识别自动化流程中的资源,这应该有助于它仅识别所需的资源 - 同时还为那些具有no_custodian_downtime标签的人提供明确的参考,以确保不需要停机时间 - 因此它可能更多很有帮助。

And if you're using YAML 如果你正在使用YAML

  Tags:
    - 'Fn::If':
      - MyCondition
      -
        Key: MyKey
        Value: MyValue
      - !Ref AWS::NoValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM