【问题标题】:how do I implement if condition in my cloudformation stack如何在我的 cloudformation 堆栈中实现 if 条件
【发布时间】:2019-02-06 22:07:28
【问题描述】:

我的堆栈环境和 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"]

【问题讨论】:

    标签: json amazon-cloudformation


    【解决方案1】:

    使用 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

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 2021-12-07
      • 2014-02-24
      • 1970-01-01
      • 2018-04-28
      • 2015-04-26
      • 2020-04-11
      • 2010-09-22
      相关资源
      最近更新 更多