簡體   English   中英

我如何在我的cloudformation堆棧中實現if條件

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

我的堆棧環境和iamprofileName中有兩個屬性。 如果我選擇非產品環境之一,即“ use1dev”,“ use1qa”。 我應該將MyPlatformEC2NonProd作為“ IAMProfileName”中的默認值

如果選擇產品環境之一,即“ useProd1”,“ useProd2”。我必須在“ IAMProfileName”中獲取MyPlatformEC2Prod作為默認值。

我該如何實現

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

使用CloudFormation條件。 例如,在您的情況下,我將執行以下操作:

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

現在,無論您想在哪里使用IAMProfileName值,都可以使用如下所示的內容,

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

有關如何使用條件句的更多信息,請查看以下鏈接。

另外,您可以使用Jinja實現更復雜的條件,只需創建一個模板並根據條件填充值即可。 但我不會對此進行詳細介紹,因為您的需求已經可以通過此方式實現。

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