AWS EC2 with Spring Boot Java — Part 1

Bhanu Prakash
4 min readJan 22, 2022

In this blog, I want to write about how we can use AWS Java SDK with Spring Boot. I created endpoints for each operation like creating an instance, creating a volume, attaching and detaching that volume from the instance. This blog can be treated as a beginner’s read who’s starting with AWS or has little experience with Amazon Web Services.

Photo by Element5 Digital on Unsplash

Pre-Requisites:
Creating an IAM-User and having appropriate permissions for EC2. You can find instructions here(https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/signup-create-iam-user.html). Get the Access Key ID and Secret Access Key as they will be needed to authenticate all our operations.

Project structure goes like this, we will have controllers for handling API calls, services to do the operations we want and also, we will have POJOs for our usage to move data around.

If you have saved the credentials file in .aws/credentials folder in windows, we don’t have to worry anything about authorization. If you would like to use another set of credentials then you may have to maintain them in a separate file and authorize manually in the code. I’ll probably write about that in another post.

Provisioning an EC2 Instance:

I picked the image ami-08e4e35cccc6189f4 which is pretty basic one and you change to whatever you want according to your subscription. I chose t2.micro as my instance type since this is only for testing, I didn’t want to provision big instances.

Let’s code

Create a spring boot project, add following dependency to your pom.xml and then boom!

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

Create a controller for our endpoints and create a service class for our business logic. I’ll have one endpoint to create an instance which looks like this

I’ll pass the image-id and instance type from a rest client. Now to initiate the process, we need to understand few things..

AmazonEC2.class

This is the class that handles all operations on an EC2 instance, starting from creation until termination. We can do bunch of things like creating a volume, attaching volume to an instance, detaching the volume, stopping and terminating a specific instance. All we need is the instance id and right information for other operations.

RunInstancesRequest.class

This is the class that creates request to create instances with the information we provided to AWS console. There are many fields we can specify to create an instance, but also there are few fields if not provided, AWS will take default values from out account. For example, if we do not provide VPC and default VPC exists in your region/availability zone it takes that up and provisions the instance. If there is no default VPC available, then the instance creation fails. This applies to security group as well.

RunInstancesResult.class

This is the class that is used to fetch the response for our request. We will get a bunch of info from this class like the instanceId, ownerId,publicIpAddress, privateIpAddress, keyName, subnetId etc. Min count and Max count are number instances to launch.

Now, let’s look at the function that creates an instance.

We’ll get 8 GB of root volume for the instance type I selected. If we want add additional volume we can do, I’ll do another post on how we can create and attach a volume to an instance in spring boot with AWS SDK.

Right then, code is done and now we run the spring boot application and then submit a request from REST client, I have added a sleep of 15000 milliseconds just to make sure we get the data after the instance creation is completed, it’s up to you to how to use this function.

If we don’t put a sleep there, then we get an instant result that the instance creation is pending. So, if we wait a bit and then try to fetch the result then we will know if the provisioning is success or not.

Alright, that’s it for this blog, as I have mentioned above, I will be writing another 2 posts, one on how to use AmazonEC2 from credentials instead of a default client and another one is about creation and attaching the volumes to an instance. Watch out for those.

Thank you for reading, please do mention any issues in the comments. Cheers!

References:

--

--