在AWS Cloud Formation模板中创建嵌套的if if

Nas*_*mad 4 amazon-web-services aws-cloudformation

我正在CloudFormation模板中使用Load Balancer创建并附加EC2实例。这里是Load Balancer资源中的实例。

"Instances" : [
    "Fn::If": ["AttachLoadBalancerToServer1",
      {
        "Fn::GetAtt": [ "ServerStack1", "Outputs.Server1" ],
      },
      {
        "Fn::If": ["AttachLoadBalancerToServer2",
        {
          "Fn::GetAtt": [ "ServerStack2", "Outputs.Server1" ],
        },""  
      ]
      },""
    ]
],
Run Code Online (Sandbox Code Playgroud)

我想在此使用if if else模式:

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

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

谢谢

Nas*_*mad 9

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

    "Instances" : [
        "Fn::If": ["AttachLoadBalancerToServer1",
          {
            "Fn::GetAtt": [ "ServerStack1", "Outputs.Server1" ],
          },
          {
            "Fn::If": ["AttachLoadBalancerToServer2",
            {
              "Fn::GetAtt": [ "ServerStack2", "Outputs.Server1" ],
            },{ "Ref" : "AWS::NoValue"}
          ]
          }
        ]
    ],
Run Code Online (Sandbox Code Playgroud)

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


小智 7

这是一个 yaml 示例

 PlacementStrategies:
    !If 
    - IsPlacementStrategyConfigured
    - 
      !If 
      - IsPlacementStrategiesConfigured
      - 
        - Type: !Ref PlacementStrategyType
          Field: !Ref PlacementStrategyField
        - Type: !Ref PlacementStrategyType2
          Field: !Ref PlacementStrategyField2
      -
        - Type: !Ref PlacementStrategyType
          Field: !Ref PlacementStrategyField
    - !Ref AWS::NoValue
Run Code Online (Sandbox Code Playgroud)

  • 难以置信。有效。我在听 SOAD 时找到了你的答案。 (2认同)