Atul
Atul

Reputation: 1590

AWS for Java SDK create EBS Volume and write data to it

I have to achieve below thing programmatically through AWS Java SDK .

  1. Create an EBS volume
  2. Upload data to it
  3. This EBS volume will not be attached to EC2 instance but AWS lambda will read data from it

I have seen example where we can create an EBS volume and attach to EC2 instance Amazon AWS creating EBS(Elastic block storage) through Java API

But it is not showing how to write data to EBS volume.

Can anybody please help me in how to do that ?

Upvotes: 0

Views: 691

Answers (1)

smac2020
smac2020

Reputation: 10714

The EC2 Client API has a method that lets you create an EBS volume. Here is the V2 example:

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.*;


/**
 * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 * 
 */
public class CreateVolume {
    public static void main(String[] args) {

      
        Region region = Region.US_EAST_1;
        Ec2Client ec2 = Ec2Client.builder()
                .region(region)
                .build();

        try {
            CreateVolumeRequest request = CreateVolumeRequest.builder()
                 .availabilityZone("us-east-1e")
                .size(384)
                .volumeType(VolumeType.GP3)
                .build();

            CreateVolumeResponse response = ec2.createVolume(request);
            System.out.println("The ARN is "+response.outpostArn());
            ec2.close();

        } catch (Ec2Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}

However, Ec2Client does not have a method that lets you write data to an an EBS volume. For information about storing data, see this doc.

https://angus.readthedocs.io/en/2014/amazon/setting-up-an-ebs-volume.html

If you have a business requirement to dynamically store/read data using a Lambda function, why not use an Amazon S3 bucket. The S3 Java API exposes methods that makes it very easy to read and write data.

Upvotes: 1

Related Questions