user3892620
user3892620

Reputation: 23

getting a handle to a load balancer using aws java sdk

If I already have an AWS ELB and want to get a handle to it using the java SDK and print the list of EC2 instances connected to it, what's the best way to do it?

I see a lot of examples on how to create an ELB using the api but nothing about getting the handle.

Thanks!

Upvotes: 0

Views: 1775

Answers (2)

user3892620
user3892620

Reputation: 23

I ended up using the following snippet:

    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonElasticLoadBalancingClient elb = new AmazonElasticLoadBalancingClient(credentials);
    DescribeLoadBalancersResult lbs = elb.describeLoadBalancers();

    List<LoadBalancerDescription> descriptions = lbs.getLoadBalancerDescriptions();
    for (LoadBalancerDescription loadBalancerDescription : descriptions) {
        System.out.println("Name: " + loadBalancerDescription.getLoadBalancerName());
        System.out.println("DNS Name: " + loadBalancerDescription.getDNSName());
        System.out.println("Instances:");

        for (Instance instance : loadBalancerDescription.getInstances()) {
            System.out.println("\t" + instance.getInstanceId());
        }
    }

Upvotes: 2

Paul Frederiksen
Paul Frederiksen

Reputation: 384

I'm fairly certain if you pass the ELB that you are trying to get the instances from to DescribeLoadBalancers will give you the information you need.

Upvotes: 0

Related Questions