Example usage for com.amazonaws.services.ec2 AmazonEC2 createVolume

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 createVolume

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 createVolume.

Prototype

CreateVolumeResult createVolume(CreateVolumeRequest createVolumeRequest);

Source Link

Document

Creates an EBS volume that can be attached to an instance in the same Availability Zone.

Usage

From source file:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 *
 * @param availabilityZone -/*w ww. jav a 2 s. c  o m*/
 * @param size - size (in Gb) of the volume to be created. Must be larger than snapshot if used
 * @param snapshotId - optional if you want to create from a snapshot
 * @param ec2Client
 * @return volumeId - the id of the newly created volume
 */
public String createEbsVolume(String availabilityZone, int size, String snapshotId, AmazonEC2 ec2Client) {
    CreateVolumeRequest request = new CreateVolumeRequest().withAvailabilityZone(availabilityZone)
            .withSize(size);

    // Only create from a snapshot if the snapshotId is not null, otherwise make new volume
    if (snapshotId != null && !snapshotId.equals("")) {
        request = request.withSnapshotId(snapshotId);
    }

    CreateVolumeResult result = ec2Client.createVolume(request);
    String volumeId = result.getVolume().getVolumeId();

    return volumeId;
}

From source file:edu.umass.cs.aws.support.AWSEC2.java

License:Apache License

/**
 * Creates a volume and attaches and mounts it on the instance at the specified mount point.
 *
 * @param ec2//from   ww  w. j a v  a2  s.c o m
 * @param instanceId
 * @param mountPoint
 * @return the id of the volume
 */
public static String createAndAttachVolume(AmazonEC2 ec2, String instanceId, String mountPoint) {
    // ATTACH A VOLUME
    Instance instance = findInstance(ec2, instanceId);
    String zone = instance.getPlacement().getAvailabilityZone();
    CreateVolumeRequest newVolumeRequest = new CreateVolumeRequest();
    newVolumeRequest.setSize(1); //1.0GB
    newVolumeRequest.setAvailabilityZone(zone);// set its available zone, it may change.

    CreateVolumeResult volumeResult = ec2.createVolume(newVolumeRequest);

    Volume v1 = volumeResult.getVolume();
    String volumeID = v1.getVolumeId();
    AttachVolumeRequest avr = new AttachVolumeRequest();//begin to attach the volume to instance
    avr.withInstanceId(instanceId);
    avr.withVolumeId(volumeID);
    avr.withDevice(mountPoint); //mount it
    ec2.attachVolume(avr);
    System.out.println("EBS volume has been attached and the volume ID is: " + volumeID);
    return (volumeID);
}