简体   繁体   English

我如何在我的cloudformation堆栈中实现if条件

[英]how do I implement if condition in my cloudformation stack

I have two attributes in my stack enviornment and iamprofileName. 我的堆栈环境和iamprofileName中有两个属性。 If I select one of the non-prod enviornment ie "use1dev","use1qa". 如果我选择非产品环境之一,即“ use1dev”,“ use1qa”。 I should get MyPlatformEC2NonProd as default value in "IAMProfileName" 我应该将MyPlatformEC2NonProd作为“ IAMProfileName”中的默认值

If I select one of the prod enviornments ie "useProd1","useProd2".I must get MyPlatformEC2Prod as default value in "IAMProfileName" 如果选择产品环境之一,即“ useProd1”,“ useProd2”。我必须在“ IAMProfileName”中获取MyPlatformEC2Prod作为默认值。

How can I achieve this 我该如何实现

"Environment" : {
        "Description" : "Environment being deployed to - use1dev, use1qa, 
use1sbox etc",
        "Type" : "String",
        "Default" : "use1sbox",
        "AllowedValues" : ["use1dev","use1qa","useProd1","useProd2"]
    },
    "IAMProfileName" : {
        "Default" : "MyPlatformEC2",
        "Type" : "String",
        "Description" : "Name of IAM profile to attach to created 
machines",
        "AllowedValues" : ["MyPlatformEC2","MyPlatformEC2NonProd"]

Use CloudFormation conditions. 使用CloudFormation条件。 For example in your case, I would do something like the following: 例如,在您的情况下,我将执行以下操作:

Conditions:
    "ProdProfileCondition": {
       "Fn::Or": [
          {"Fn::Equals": ["useProd1", {"Ref": "Environment"}]},
          {"Fn::Equals": ["useProd2", {"Ref": "Environment"}]},
       ]
    }

Now wherever you want to use the IAMProfileName value, use something like the following, 现在,无论您想在哪里使用IAMProfileName值,都可以使用如下所示的内容,

SomeAWSResource:
Properties:
    "ProfileName" : [{
      "Fn::If" : [
        "ProdProfileCondition",
        {"Ref" : "MyPlatformEC2"},
        {"Ref" : "MyPlatformEC2NonProd"}
      ]
    }] 

For more information on how to use conditionals, check out the following link. 有关如何使用条件句的更多信息,请查看以下链接。

Also, you can achieve more complicated conditionals using Jinja, just create a template and fill values according to conditions. 另外,您可以使用Jinja实现更复杂的条件,只需创建一个模板并根据条件填充值即可。 But I wouldn't go into details of that because what you need can be fulfilled by this already. 但我不会对此进行详细介绍,因为您的需求已经可以通过此方式实现。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html

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

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