Search in sources :

Example 1 with AttachVolumeRequest

use of com.amazonaws.services.ec2.model.AttachVolumeRequest in project GNS by MobilityFirst.

the class AWSEC2 method createAndAttachVolume.

/**
   * Creates a volume and attaches and mounts it on the instance at the specified mount point.
   *
   * @param ec2
   * @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();
    //1.0GB
    newVolumeRequest.setSize(1);
    // set its available zone, it may change.
    newVolumeRequest.setAvailabilityZone(zone);
    CreateVolumeResult volumeResult = ec2.createVolume(newVolumeRequest);
    Volume v1 = volumeResult.getVolume();
    String volumeID = v1.getVolumeId();
    //begin to attach the volume to instance
    AttachVolumeRequest avr = new AttachVolumeRequest();
    avr.withInstanceId(instanceId);
    avr.withVolumeId(volumeID);
    //mount it
    avr.withDevice(mountPoint);
    ec2.attachVolume(avr);
    System.out.println("EBS volume has been attached and the volume ID is: " + volumeID);
    return (volumeID);
}
Also used : CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) Instance(com.amazonaws.services.ec2.model.Instance) Volume(com.amazonaws.services.ec2.model.Volume) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest)

Aggregations

AttachVolumeRequest (com.amazonaws.services.ec2.model.AttachVolumeRequest)1 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)1 CreateVolumeResult (com.amazonaws.services.ec2.model.CreateVolumeResult)1 Instance (com.amazonaws.services.ec2.model.Instance)1 Volume (com.amazonaws.services.ec2.model.Volume)1