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);
}
Aggregations