'Is there an API call in AWS Elastic Beanstalk to have a list of running hosts? (with IPs)

The title really says it all. I need each instance running to be aware of what the other instances are, including their IP addresses. Is there an EB API call to do that? Or maybe a more generic way in AWS?



Solution 1:[1]

The AWS Elastic Beanstalk SDK exposes methods that provide details about both the Environment and Applications. (Not clear what you mean by I need each instance running to be aware of what the other instances are)

Anyhow -- you can get a list of applications and get details about those EB apps. For example, you can get the status (an EB app that is running will have a status value Ready) and the app URL (not the IP). SO you can get at the details you are looking for with the SDK.

Running this code returns details that you see the AWS Management Console -- such as:

  • The application name is VideoAnalyzer
  • The Environment ARN is arn:aws:elasticbeanstalk:us-east-1:xxxxxx047983:environment/VideoAnalyzer/Videoanalyzer-env
  • The Endpoint URL is awseb-AWSEB-xxxxxxY1BQF02-xxxx6689.us-east-1.elb.amazonaws.com
  • The status is Ready

I will show you the code in Java SDK v2.

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.elasticbeanstalk.ElasticBeanstalkClient;
import software.amazon.awssdk.services.elasticbeanstalk.model.DescribeApplicationsResponse;
import software.amazon.awssdk.services.elasticbeanstalk.model.ApplicationDescription;
import software.amazon.awssdk.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
import software.amazon.awssdk.services.elasticbeanstalk.model.DescribeEnvironmentsResponse;
import software.amazon.awssdk.services.elasticbeanstalk.model.EnvironmentDescription;
import software.amazon.awssdk.services.elasticbeanstalk.model.ElasticBeanstalkException;
import java.util.List;
//snippet-end:[eb.java2.describe_app.import]


/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */

public class DescribeApplications {

    public static void main(String[] args) {

        Region region = Region.US_EAST_1;
        ElasticBeanstalkClient beanstalkClient = ElasticBeanstalkClient.builder()
                .region(region)
                .build();

        describeApps(beanstalkClient);
    }

    //snippet-start:[eb.java2.describe_app.main]
    public static void describeApps(ElasticBeanstalkClient beanstalkClient) {

        try {
            DescribeApplicationsResponse applicationsResponse = beanstalkClient.describeApplications();
            List<ApplicationDescription> apps = applicationsResponse.applications();
            for (ApplicationDescription app: apps) {
                System.out.println("The application name is "+app.applicationName());
                DescribeEnvironmentsRequest desRequest =  DescribeEnvironmentsRequest.builder()
                            .applicationName(app.applicationName())
                            .build();

                DescribeEnvironmentsResponse res = beanstalkClient.describeEnvironments(desRequest) ;
                List<EnvironmentDescription> envDesc = res.environments();
                for (EnvironmentDescription desc: envDesc) {
                    System.out.println("The Environment ARN is "+desc.environmentArn());
                    System.out.println("The Endpoint URL is "+ desc.endpointURL());
                    System.out.println("The status is "+ desc.status());
                }
            }
        } catch (ElasticBeanstalkException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
    //snippet-end:[eb.java2.describe_app.main]
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1