Spring Cloud AWS - EC2

Last Updated : 6 May, 2026

Spring Cloud AWS simplifies the integration of Spring applications with Amazon Web Services by providing ready-to-use abstractions and APIs. It allows developers to interact with AWS services like EC2 without handling infrastructure complexities. This enables faster development and seamless cloud-based deployments.

  • Provides easy integration with AWS services using familiar Spring APIs.
  • Reduces infrastructure management by leveraging managed cloud services.
  • Supports scalable application deployment using services like EC2.

Prerequisites

  • Java 8 or higher
  • AWS account
  • Basic knowledge of Spring Boot

Steps to Implements Spring Cloud AWS - EC2

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

Step 1: Create a Spring Boot Project

Generate a project using Spring Initializr with required dependencies.

Step 2: Spring Cloud AWS Maven dependency

Add Spring Cloud AWS dependency in pom.xml to enable AWS integration.

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-aws-context</artifactId>

<version>{spring-cloud-version}</version>

</dependency>

</dependencies>

Step 3: Configure AWS Credentials

Set AWS credentials using application.properties or environment variables (Access Key & Secret Key).

cloud.aws.credentials.access-key=YOUR_ACCESS_KEY
cloud.aws.credentials.secret-key=YOUR_SECRET_KEY
cloud.aws.region.static=ap-south-1

Step 4: Enable EC2 Instance Metadata

Enable metadata support using configuration.

import org.springframework.context.annotation.Configuration;
import io.awspring.cloud.context.config.annotation.EnableContextInstanceData;

@Configuration
@EnableContextInstanceData
public class EC2Config {

}

Step 5: Inject EC2 Metadata Values

Fetch EC2 instance details like AMI ID and instance type.

@Value("${ami-id}")

private String amiId;

@Value("${instance-type}")

private String instanceType;

@Value("${hostname}")

private String hostname;

Step 6: Create EC2 Client

Use AWS EC2 client to interact programmatically.

import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import org.springframework.context.annotation.Bean;

@Bean
public AmazonEC2 amazonEC2() {
return AmazonEC2ClientBuilder.defaultClient();
}

Step 7: Use Custom EC2 Tags (Optional)

Enable user-defined tags in XML config

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

In Spring Boot, custom tags can be accessed using SpEL without XML configuration.Inject tag value:

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

Step 8: Launch EC2 Instance (AWS Console Step)

Now deploy the Spring Boot application on an EC2 instance to make it accessible over the internet.

  • Go to AWS Console
  • Choose AMI, instance type
  • Create key pair
  • Configure security group (allow port 8080)

Step 9: Connect to EC2 Instance

Connect to the EC2 instance securely using SSH with the generated key pair.

ssh -i key.pem ec2-user@<public-ip>

Step 10: Deploy Spring Boot Application

Upload the JAR and run the application

java -jar app.jar

Step 11: Access Application

Access the deployed Spring Boot application using the EC2 public IP and configured port.

http://<EC2-Public-IP>:8080

Comment

Explore