繁体   English   中英

在 AWS Cloud Formation 模板中创建嵌套 If else

[英]Create Nested If else in AWS Cloud Formation Template

我在我的 CloudFormation 模板中使用 Load Balancer 创建和附加 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 结构吗? 我可以添加一个条件,但无法找到如何在一个条件下使用第二个条件。

谢谢

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

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

这对我很有用。 我发布了答案,因为如果有人在将来遇到同样的问题我的回答可能对他有帮助。

这是一个 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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