Example usage for com.amazonaws.services.ec2.model VolumeType Standard

List of usage examples for com.amazonaws.services.ec2.model VolumeType Standard

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model VolumeType Standard.

Prototype

VolumeType Standard

To view the source code for com.amazonaws.services.ec2.model VolumeType Standard.

Click Source Link

Usage

From source file:edu.brandeis.wisedb.aws.VMCreator.java

License:Open Source License

public VM createInstance(VMType type, VMDiskConfiguration disk, boolean waitForRunning)
        throws VirtualMachineException {
    AmazonEC2Client ec2 = getEC2();/* w  ww . ja  va 2s  . c  om*/

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest();

    // TODO: figure out how to change storage type

    String instanceType = "";
    switch (type) {
    case C4_LARGE:
        instanceType = "c4.large";
        break;
    case C4_XLARGE:
        instanceType = "c4.xlarge";
        break;
    case M3_LARGE:
        instanceType = "m3.large";
        break;
    case M3_MEDIUM:
        instanceType = "m3.medium";
        break;
    case T2_MEDIUM:
        instanceType = "t2.medium";
        break;
    case T2_SMALL:
        instanceType = "t2.small";
        break;
    default:
        break;

    }

    BlockDeviceMapping bdm = null;
    switch (disk) {
    case HD100:
        bdm = new BlockDeviceMapping().withDeviceName("/dev/sda1")
                .withEbs(new EbsBlockDevice().withVolumeSize(100).withVolumeType(VolumeType.Standard)
                        .withDeleteOnTermination(true).withSnapshotId(config.getSnapshotID()));

    case SSD10:
        bdm = new BlockDeviceMapping().withDeviceName("/dev/sda1")
                .withEbs(new EbsBlockDevice().withVolumeSize(10).withVolumeType(VolumeType.Gp2)
                        .withDeleteOnTermination(true).withSnapshotId(config.getSnapshotID()));
    case SSD30:
        bdm = new BlockDeviceMapping().withDeviceName("/dev/sda1")
                .withEbs(new EbsBlockDevice().withVolumeSize(30).withVolumeType(VolumeType.Gp2)
                        .withDeleteOnTermination(true).withSnapshotId(config.getSnapshotID()));
    default:
        break;

    }

    System.out.println(instanceType);
    runInstancesRequest = runInstancesRequest.withImageId(config.getAMIID()).withInstanceType(instanceType)
            .withMinCount(1).withMaxCount(1).withKeyName(config.getKeyPairName())
            .withSubnetId(config.getSubnet()).withBlockDeviceMappings(bdm);

    RunInstancesResult rir = ec2.runInstances(runInstancesRequest);

    String instanceID = rir.getReservation().getInstances().get(0).getInstanceId();
    String ip;

    if (waitForRunning) {
        int maxTry = 60;
        while (true) {
            try {
                DescribeInstancesResult dir = ec2
                        .describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceID));
                InstanceState is = dir.getReservations().get(0).getInstances().get(0).getState();
                //System.out.println("Got state: " + is);

                // apparently this constant isn't stored anywhere... *sigh*
                if (is.getCode() == 16) {
                    ip = dir.getReservations().get(0).getInstances().get(0).getPublicIpAddress();
                    break;
                }
            } catch (AmazonServiceException e) {
                //System.err.println("Trouble with AWS: " + e.getMessage());
            }
            maxTry--;

            if (maxTry == 0) {
                throw new VirtualMachineException("machine never entered running state");
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }
        }
        VM toR = new VM(instanceID, ip, this);
        return toR;
    }

    VM toR = new VM(instanceID, null, this);
    return toR;
}