MayoMan
MayoMan

Reputation: 4917

Can I tell what AWS loadbalancer my EC2 instance is associated with?

The problem I am trying to solve is how to make my code running within an EC2 instance which is part of a load balanced AWS cluster aware of how many other EC2 instances are withing the same cluster/loadbalancer.

I have the following code which when given the name of a LoadBalancer can tell me how many EC2 instances are associated with that Loadbalancer.

DescribeLoadBalancersResult dlbr = loadBalancingClient.describeLoadBalancers();
List<LoadBalancerDescription> lbds = dlbr.getLoadBalancerDescriptions();
for( LoadBalancerDescription lbd : lbds )
{
    if( lbd.getDNSName().equalsIgnoreCase("MyLoadBalancer"))
    {
        System.out.println(lbd.getDNSName() + " has " + lbd.getInstances().size() + " instances") ;
    }
}

which works fine and prints out the loadbalancer name and number of instances is has associated with it.

However I want to see if I can get this info without having to provide the Loadbalancer name. In our setup an EC2 instance will only ever be associated with one Loadbalancer so is there any way to go back the way from EC2 instance to Loadbalancer?

I figure I can go down the route of getting all loadbalancers from All regions, iterating through them until I find the one that contains my EC2 instance but I figured there might be an easier way?

Upvotes: 3

Views: 9782

Answers (1)

Neal Magee
Neal Magee

Reputation: 1701

An interesting challenge -- I would have to wrangle with the code myself to think this through, but my gut first response would be to use the AWS CLI here, and to just invoke it from within your Java/C#.

You can make this call:

  aws elb describe-load-balancers

And get all manner of information about any and all ELBs, and could simply --query filter that by the instance ID of the instance making the call anyway -- in order to find out what other friends the instance has joined to its same ELB. Just call the internal instance metadata to get that ID:

  http://169.254.169.254/latest/meta-data/instance-id

Or another fun way to go would be to bootstrap your instance AMIs so that when they are spawned and joined to an ELB, they register themselves in a SimpleDB or DynamoDB table. We do this all the time as a way of keeping current inventories of websites, or software installed, etc. So this way you would have a list, which you could then keep trimmed by checking for "running" status.

EDIT - 4/13/2015

@MayoMan I have hadto make use of this as well in some current work -- to identify healthy instances attached to an ELB in an auto-scaling group and then act upon them. I've found 'jq' to be a really helpful command-line tool. You could also make these calls directly to an ELB, but here it's describing an ASG:

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names <ASG Name> | jq -r .AutoScalingGroups[0].Instances[0].HealthStatus

Or to list the InstanceIds themselves:

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names <ASG Name> | jq -r .AutoScalingGroups[0].Instances[0-3].InstanceId

Upvotes: 2

Related Questions