Create Nested If else in AWS Cloud Formation Template

11,629

I achieved the nested IF by adding the following structure in AWS CloudFormaiton template:

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

This worked well for me. I am posting answer because if someone stuck in future about the same problem my answer might help him.

Share:
11,629

Related videos on Youtube

Nasir Ahmad
Author by

Nasir Ahmad

I am a passionate software developer with a vast experience including development operations (DevOps), AWS Services, Monitoring tools like DataDog, ELk who started his career as online marketing expert back in 2011. After that explored Web Development, Programming and more. I specialize in Laravel, PHP, JAVA, MVC based applications, REST full services and DevOps! – not only for web solutions but also i participate in improving website performance according to the Google and other professional standards to optimize. Besides that i have worked on various types of deployments (Flask, MatterMost, Maven, Laravel, Sendy, WordPress, CackePHP, CodeIgniter etc) My project count is well into the 20s, with clients located all over the world, but I specialize in meeting the needs of all companies and individuals who are looking to automate their businesses and want web services and web related solutions. My focus is on developing practical business solutions, using a wide array of mature and proven plugins, components and 3rd party solutions to solve common small to large scale ERP business requirements. I'd be more than happy to offer you my services, so feel free to contact me for an interview. I look forward to hearing from you.

Updated on September 14, 2022

Comments

  • Nasir Ahmad
    Nasir Ahmad over 1 year

    I am creating and attaching EC2 instance with Load Balancer in my CloudFormation template. here instances in Load Balancer resource.

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

    I want to use this if else pattern in this:

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

    Any body can help me writing IF ELSEIF structure in this template? I am able to add one condition but not able to find how to use the second condition within one condition.

    Thank you