简体   繁体   中英

how do I implement if condition in my cloudformation stack

I have two attributes in my stack enviornment and iamprofileName. If I select one of the non-prod enviornment ie "use1dev","use1qa". I should get MyPlatformEC2NonProd as default value in "IAMProfileName"

If I select one of the prod enviornments ie "useProd1","useProd2".I must get MyPlatformEC2Prod as default value in "IAMProfileName"

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. 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,

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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