EC2 Volumes in AWS with JAVA SDK

Bhanu Prakash
4 min readMar 11, 2022
Photo by JOSHUA COLEMAN on Unsplash

Hi There! In this blog, I want to show how we can add an externally created disk and attach it to a virtual machine that is already up and running. Let’s dig in.

I might use some setup that I already have from the previous post. You can go through it below.

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. And a VM that’s already provisioned in AWS.

Alright, so our goal is to create a disk from Java SDK and attach it to a virtual machine.

There are 2 steps here:

  1. Creating a Disk
  2. Attaching the disk to a VM

We could do both of them in a single step in a single function but for better understanding it would be nice to go one step at a time.

Creating a Disk:

We’ll see creating a disk first, there is a class CreateVolumeRequest.java in the Java SDK for AWS which handles all the requests to create the volume. It has bunch of options to provide, size, type, zone and the list goes on. It depends on how we want to use the disk. And the code goes like below

Obviously, we will be needing AmazonEC2 client to gather our credentials and connect to our AWS account. If you see, we just have to create an instance of CreateVolumeRequest and use the amazing setters in the object and set our values. Since this is a practice code, I hardcoded them.

But if you need this kind of setup in your projects you will be passing values from UI or some other module in that case you don’t need to hardcode anything. Also, there are APIs from AWS that provide values for volume types, zones etc. (Look for objects that starts with Describe* Ex: DescribeAvailabilityZonesList).

One thing to note here is availabilityZone of the disk should be same as instance’s.

The last line of the above snippet gets the response from AWS whether disk is created. (CreateVolumeResponse). From this object we will be able to get the volume object which contains all the information like size, volumeId, state etc.

Our Step 1 is done. Moving on to Step 2. Output from the API looks something like this

We need to focus on the “volume” object, where you will see several details. We can see the state is creating, after couple of minutes you could check on the AWS portal whether the creation is completed.

Attaching the disk to VM:

For this we will use AttachVolumeRequest object to attach a volume to an instance. We got the volumeId from output from step 1 result. We also need to get instanceId from AWS portal or somewhere in your database if you have stored the instance details. All we need to do is create an object of AttachVolumeRequest, use the setters to set the required information like instanceId, deviceName and availabilityZone.

After everything is set, we need to run ec2.attachVolume(attachVolumeRequest) and the response is you might’ve guessed it by now. Response Object will be AttachVolumeResult. Detaching the volume also works in the same way, we just need to provide instanceid and volumeId to the DetachVolumeRequest object and boom!

Result looks like below

We have to focus on the attachment object, we can see the deviceName, instanceId and the state as well.

Well, we’ve reached the end of this post, you can try with several volumes and different types of instance types and see what happens.

Thank you for reading, please leave a comment if you think something can be improved or can be done in a different way. Cheers!

References:

--

--