【问题标题】:Create Nested If else in AWS Cloud Formation Template在 AWS Cloud Formation 模板中创建嵌套 If else
【发布时间】:2018-12-07 19:43:46
【问题描述】:

我正在我的 CloudFormation 模板中使用负载均衡器创建和附加 EC2 实例。 这里是负载均衡器资源中的实例。

"Instances" : [
    "Fn::If": ["AttachLoadBalancerToServer1",
      {
        "Fn::GetAtt": [ "ServerStack1", "Outputs.Server1" ],
      },
      {
        "Fn::If": ["AttachLoadBalancerToServer2",
        {
          "Fn::GetAtt": [ "ServerStack2", "Outputs.Server1" ],
        },""  
      ]
      },""
    ]
],

我想在这个中使用这个 if else 模式:

if(AttachLoadBalancerToServer1){
"Instances" =  "Fn::GetAtt": [ "ServerStack1", "Outputs.Server1" ],
} 
else if(AttachLoadBalancerToServer2){
"Instances" =  "Fn::GetAtt": [ "ServerStack2", "Outputs.Server1" ],
}
else{
"Instances" =  "",
}

任何机构都可以帮助我在此模板中编写 IF ELSEIF 结构吗?我能够添加一个条件,但无法找到如何在一个条件中使用第二个条件。

谢谢

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    我通过在 AWS CloudFormaiton 模板中添加以下结构来实现嵌套 IF:

        "Instances" : [
            "Fn::If": ["AttachLoadBalancerToServer1",
              {
                "Fn::GetAtt": [ "ServerStack1", "Outputs.Server1" ],
              },
              {
                "Fn::If": ["AttachLoadBalancerToServer2",
                {
                  "Fn::GetAtt": [ "ServerStack2", "Outputs.Server1" ],
                },{ "Ref" : "AWS::NoValue"}
              ]
              }
            ]
        ],
    

    这对我来说效果很好。我发布答案是因为如果有人在未来遇到同样的问题,我的答案可能会对他有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2021-10-26
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多