ehime
ehime

Reputation: 8415

Adding instances behind multiple LoadBalancers in CloudFormation?

I am trying to figure out a way to add a ec2 node to two load balancers, but am not finding a sane way to do it. I would think that the below code would allow me to do this, but am not getting my expected results

Please note that this code is HIGHLY stripped down to just show the LB portion.

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": " App Demo / Jd / 01042016",
  "Parameters": {
    "LoadBalancers": {
      "Description": "Please refer the LoadBalancerNames which you want the instances to be registered to",
      "Type": "List<AWS::EC2::LoadBalancers::LoadBalancerNames>",
      "Default": "web-app-demo-ext,web-api-demo-ext"
    }
  },
  "Resources": {
    "AppAutoScalingGroup": {
      "Type": "AWS::AutoScaling::AutoScalingGroup",
      "Properties": {
        "AvailabilityZones": {
          "Ref": "AZs"
        },
        "LoadBalancerNames": [{
          "Ref": "LoadBalancers"
        }]
      }
    }
  }
}

I would expect to see a list which I can add multiple LBs to, but I am instead seeing a normal input box (string)

Adding in images with some firebug hackery....

Expected Results Expected Results

What actually happens What I actually get

UPDATE

After fudging with Cloud Designer, I believe that "Type": "List<AWS::EC2::LoadBalancers::LoadBalancerNames>" is incorrect and should really be "Type": "List<AWS::ElasticLoadBalancing::LoadBalancer>"

This still does not propagate as a list though

Upvotes: 1

Views: 426

Answers (2)

Matt Houser
Matt Houser

Reputation: 36093

This won't give you the UI you were looking for, but it will allow you to specify multiple load balancer names in a single parameter.

Use CommaDelimitedList as the parameter type.

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": " App Demo / Jd / 01042016",
  "Parameters": {
    "LoadBalancers": {
      "Description": "Please refer the LoadBalancerNames which you want the instances to be registered to",
      "Type": "CommaDelimitedList",
      "Default": "web-app-demo-ext,web-api-demo-ext"
    }
  },
  "Resources": {
    "AppAutoScalingGroup": {
      "Type": "AWS::AutoScaling::AutoScalingGroup",
      "Properties": {
        "AvailabilityZones": {
          "Ref": "AZs"
        },
        "LoadBalancerNames": {
          "Ref": "LoadBalancers"
        }
      }
    }
  }
}

Upvotes: 1

ehime
ehime

Reputation: 8415

Figured it out. You will need multiple references. And the reason is because the "LoadBalancerNames" key can take multiple references, like so: "LoadBalancerNames": { "Foo", "Bar" }

Really hope this helps someone else as it had me scratching my head for days :)

{
  ................
  "Parameters": {
    .......
    "LoadBalancerApp": {
      "Description": "Please refer the LoadBalancerNames (APP) which you want the instances to be registered to",
      "Type": "String",
      "Default": "web-app-demo-ext"
    },
    "LoadBalancerApi": {
      "Description": "Please refer the LoadBalancerNames (API) which you want the instances to be registered to",
      "Type": "String",
      "Default": "web-api-demo-ext"
    },
    .......
  },
  "Resources": {
    ........
    "AppnameAutoScalingGroup": {
      "Type": "AWS::AutoScaling::AutoScalingGroup",
      "Properties": {
        "AvailabilityZones": {
          "Ref": "AZs"
        },
        "LaunchConfigurationName": {
          "Ref": "AppnameLaunchConfig"
        },
        "LoadBalancerNames": [{
          "Ref": "LoadBalancerApp"
        }, {
          "Ref": "LoadBalancerApi"
        }],
    ..........
}

Upvotes: 1

Related Questions