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:

Hướng dẫn Java Design Pattern – Composite
Derived Query Methods in Spring Data JPA Repositories
Hamcrest Collections Cookbook
Java Program to find the peak element of an array using Binary Search approach
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Fibonacci Heap
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Comparing Dates in Java
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Check whether Directed Graph is Connected using DFS
The StackOverflowError in Java
Java Program to Implement Gauss Seidel Method
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Hướng dẫn Java Design Pattern – Bridge
Refactoring Design Pattern với tính năng mới trong Java 8
Unsatisfied Dependency in Spring
Remove the First Element from a List
Java Program to Implement ConcurrentSkipListMap API
Java Program to Implement IdentityHashMap API
Java Program to Solve the Fractional Knapsack Problem
Object cloning trong java
Jackson Exceptions – Problems and Solutions
Configure a Spring Boot Web Application
Giới thiệu Design Patterns
Extract links from an HTML page
A Guide to Java 9 Modularity
Lập trình đa luồng với Callable và Future trong Java
Lấy ngày giờ hiện tại trong Java
Java Program to Implement Variable length array