1

Is there a way to automatically determine what the load balancer, if any, of a given EC2 instance is? I.e., have it ask "who is my LB and what is its service IP?". I was thinking of the instance metadata service but the LB "in front" of the instance does not seem to be listed there.

The reason for this is that I want to set ELB address (or DNS name) as a Chef node attribute to be automatically queried by client services that need to know this external IP.

1 Answers1

2

You can get the information using the API.

  1. Get the current EC2 instance ID from the EC2 instance metadata.
  2. Use elasticloadbalancing:DescribeLoadBalancers to get a list of load balancers.
  3. Iterate through each load balancer in the response. For each load balancer, iterate through the list of instances looking for your instance ID.

Note #1: It's possible for an EC2 instance to be registered with none, one, or even two or more load balancers.

Note #2: The external IP address of an Elastic Load Balancer most likely will change over time.

Matt Houser
  • 10,053
  • 1
  • 28
  • 28