Retrieving EC2 instances associated with ELB
I have been working at the rapid changing software development field over 19 years as a software engineer, architect, devops. I am an expert in designing and implementing scalable architectures to deal with large transactions and massive requests on e-commerce and messaging services. I am skillful at using opensource technologies like Node.js, Nginx, Redis, Cassandra, ELK and Kafka to secure site reliability. I also have proficiency in C#, Java, Javascript, Python and Go.
AWS SDK provides an easy way to retrieve EC2 instances which is associated with ELB.
You only need a couple of APIs to get it done. First, get a ELB list through describeLoadBalancers() to choose your ELB then call describeInstances() to get EC2 instances associate with it.
describeLoadBalancers() example
AmazonElasticLoadBalancing elbClient = AmazonElasticLoadBalancingClient
.builder()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.withRegion(Regions.EU_WEST_1)
.build();
DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(
new DescribeLoadBalancersRequest());
for (LoadBalancer lb : result.getLoadBalancers()) {
System.out.println(lb.getLoadBalancerName());
}