Spring Cloud AWS – EC2

1. EC2 Metadata Access

The AWS EC2MetadataUtils class provides static methods to access instance metadata like AMI Id and instance type. With Spring Cloud AWS we can inject this metadata directly using the @Value annotation.

This can be enabled by adding the @EnableContextInstanceData annotation over any of the configuration classes:

@Configuration
@EnableContextInstanceData
public class EC2EnableMetadata {
    //
}

In a Spring Boot environment, instance metadata is enabled by default which means this configuration is not required.

Then, we can inject the values:

@Value("${ami-id}")
private String amiId;

@Value("${hostname}")
private String hostname;

@Value("${instance-type}")
private String instanceType;

@Value("${services/domain}")
private String serviceDomain;

1.1. Custom Tags

Additionally, Spring also supports injection of user-defined tags. We can enable this by defining an attribute user-tags-map in context-instance-data using the following XML configuration:

<beans...>
    <aws-context:context-instance-data user-tags-map="instanceData"/>
</beans>

Now, let’s inject the user-defined tags with the help of Spring expression syntax:

@Value("#{instanceData.myTagKey}")
private String myTagValue;

2. EC2 Client

Furthermore, if there are user tags configured for the instance, Spring will create an AmazonEC2 client which we can inject into our code using @Autowired:

@Autowired
private AmazonEC2 amazonEc2;

Please note that these features work only if the app is running on an EC2 instance.

3. Conclusion

This was a quick and to-the-point introduction to accessing EC2d data with Spring Cloud AWS.

In the next article of the series, we’ll explore the RDS support.

As usual, the examples are available over on GitHub.

Related posts:

Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Custom HTTP Header with the HttpClient
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Implement Vector API
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Check the Connectivity of Graph Using BFS
Java Program to Describe the Representation of Graph using Adjacency List
Request Method Not Supported (405) in Spring
Java Program to implement Bit Matrix
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Serialize Only Fields that meet a Custom Criteria with Jackson
Guide to the Fork/Join Framework in Java
Hướng dẫn Java Design Pattern – Proxy
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement PrinterStateReasons API
Spring Boot - Unit Test Cases
Guide to the Java TransferQueue
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Jackson Ignore Properties on Marshalling
Batch Processing with Spring Cloud Data Flow
The Modulo Operator in Java
Filtering and Transforming Collections in Guava
Java Program to Implement DelayQueue API
A Guide to JUnit 5
Java Program to Perform Right Rotation on a Binary Search Tree
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
A Guide to the Java LinkedList
Working With Maps Using Streams