Open In App

Spring Cloud AWS – EC2

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Spring Cloud for AWS (Amazon Web Services) component makes integrating with hosted Amazon Web Services easier. It provides an easy method to use popular Spring idioms and APIs, such as the messaging or caching API, to communicate with AWS-provided services. Without worrying about infrastructure or maintenance, developers can design their applications around the hosted services.

In this article, we will be learning the implementation of EC2 (Elastic Compute Cloud) in Spring Cloud AWS.

Implementation of Spring Cloud AWS – EC2

Below are the steps to implement EC2 in Spring Cloud AWS.

Step 1: Spring Cloud AWS Maven dependency

Maven users can directly utilize Spring Cloud AWS module dependencies by configuring the specific module. All of the Spring modules’ transitive dependencies as well as the Amazon SDK required to run the modules are included in the Spring Cloud AWS module.

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-context</artifactId>
<version>{spring-cloud-version}</version>
</dependency>
</dependencies>

Step 2: Simple Amazon S3 Download

First, let’s see how simple it is to access files on S3:

Java




@Autowired
ResourceLoader resourceLoader;
  
/**
 * Downloads an object from Amazon S3 using the provided S3 URL.
 * @throws IOException If an I/O error occurs during the download.
 */
public void downloadS3Object(String s3Url) throws IOException {
    // Obtain a Resource object from the ResourceLoader using the S3 URL.
    Resource resource = resourceLoader.getResource(s3Url);
  
    // Create a File object to represent the downloaded S3 object.
    File downloadedS3Object = new File(resource.getFilename());
    
    try (InputStream inputStream = resource.getInputStream()) {
        // Copy the contents of the InputStream to the local file.
        Files.copy(inputStream, downloadedS3Object.toPath(), 
          StandardCopyOption.REPLACE_EXISTING);
    }
}


  • First, we need to download an object from Amazon S3 using the provided S3 URL.
  • Then, obtain a Resource object from the ResourceLoader using the S3 URL.
  • After that we will create a File object to represent the downloaded S3 object.
  • At last, copy the contents of the InputStream to the local file.

Step 3: Access to the EC2 Metadata

Static methods for accessing instance metadata, such as AMI Id and instance type, are provided by the AWS EC2MetadataUtils class. We may use the @Value annotation in Spring Cloud AWS to directly insert this metadata. The @EnableContextInstanceData annotation may be added to any configuration class to enable this:

@Configuration
@EnableContextInstanceData
public class EC2EnableMetadata {
//....
}

Step 4: Inject the values in EC2 Metadata

As instance metadata is enabled by default in a Spring Boot environment, this setup is not necessary. Next, the values may be injected:

Java




@Value("${ami-id}")
private String amiId;
  
@Value("${hostname}")
private String hostname;
  
@Value("${instance-type}")
private String instanceType;
  
@Value("${services/domain}")
private String serviceDomain;
  
/**
 * Retrieves the AMI ID from the application configuration.
 
 * @return The AMI ID as a String.
 */
public String getAmiId() {
    return amiId;
}
  
/**
 * Retrieves the hostname from the application configuration.
 
 * @return The hostname as a String.
 */
public String getHostname() {
    return hostname;
}
  
/**
 * Retrieves the instance type from the application configuration.
 
 * @return The instance type as a String.
 */
public String getInstanceType() {
    return instanceType;
}
  
/**
 * Retrieves the service domain from the application configuration.
 
 * @return The service domain as a String.
 */
public String getServiceDomain() {
    return serviceDomain;
}


Step 5: Use Custom Tags

Spring allows user-defined tag injection. By creating an attribute user-tags-map in context-instance-data with the following XML configuration, we may enable this:

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

Step 6: Insert the user-defined tags

Let’s now utilize the Spring expression syntax to inject the user-defined tags:

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

Step 7: Create EC2 Client

If the instance has user tags enabled, Spring will generate an AmazonEC2 client that we can use @Autowired to inject into our code:

@Autowired
private AmazonEC2 amazonEc2;


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads