Talk with AWS APIs – AWS SDK for Java


So, you have your application deployed on AWS (maybe is just an EC2 instance or a beanstalk environment) and you need the application to know specific data about the environment it is deployed.

Example given, the app needs to know if it is running on blue or green environment. These two environments are identical and the only difference between them is that blue is running the current application version and green is running the new application version.

In this case you could use AWS APIs to find the CNAME of the application.


For the start we need to add the maven dependencies in pom.xml.

<dependency>
 <groupId>com.amazonaws</groupId>
 <artifactId>aws-java-sdk-elasticbeanstalk</artifactId>
 <version>1.12.415</version>
</dependency>

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-ec2</artifactId>
   <version>1.12.414</version>
</dependency>

Having the AWS libs downloaded, we need to create the AmazonEC2Client to retrieve all info we need form EC2

AmazonEC2Client ec2Client = (AmazonEC2Client) AmazonEC2ClientBuilder.standard()
                .withCredentials(new CustomAWSCredentialsProvider())
                .withRegion(Regions.EU_CENTRAL_1)
                .build();

and an AWSElasticBeanstalkClient

AWSElasticBeanstalkClient beanstalkClient = (AWSElasticBeanstalkClient) AWSElasticBeanstalkClientBuilder.standard()
                .withCredentials(new CustomAWSCredentialsProvider())
                .withRegion(Regions.EU_CENTRAL_1)
                .build();

Get instanceId of EC2 instance the app is running.

final String instanceId = EC2MetadataUtils.getInstanceId();

Given the instanceId we will use the ec2Client to get the tags of ec2 instance with this instanceId.

final DescribeTagsRequest req = new DescribeTagsRequest().withFilters(new Filter("resource-id", Collections.singletonList(instanceId)));

final DescribeTagsResult describeTagsResult = ec2Client.describeTags(req);

final List<TagDescription> tags = describeTagsResult.getTags();

final String envName = tags
     .stream()
     .filter(it -> it.getKey().equalsIgnoreCase("Name"))
     .map(it -> it.getValue())
     .findAny()
     .orElse("");

Now envName has the value of Tag=”Name”. Using the beanstalkClient we will find all environments are running on our AWS account, we will filter them based on envName.

final List<EnvironmentDescription> environments = beanstalkClient.describeEnvironments().getEnvironments();

final String cname = environments.stream()
            .filter(it ->     it.getEnvironmentName().equalsIgnoreCase(envName))
                .map(it -> it.getCNAME())
                .findAny()
                .orElse("");

Great! Having the cname we could know if our app is running on blue env!


You could check the implementation code on

https://github.com/despoina555/CodeExamples/blob/main/src/main/java/aws/AwsClient.java

and the test in

https://github.com/despoina555/CodeExamples/blob/main/src/test/java/org/despina/AwsTest.java