如何使用java sdk将新的EBS卷附加到现有的EC2实例?

时间:2013-04-11 16:03:10

标签: java amazon-web-services amazon-ec2

我已尝试使用AttachVolumeRequest,但作为回应我收到以下错误

 Caught Exception: The request must contain the parameter volume
 Reponse Status Code: 400
 Error Code: MissingParameter

这是我的代码,在这段代码中,ec2是我的amazonclient对象,到目前为止工作正常

AttachVolumeRequest attachRequest=new AttachVolumeRequest()
    .withInstanceId("my instance id");
attachRequest.setRequestCredentials(credentials);

EbsBlockDevice ebs=new EbsBlockDevice();
ebs.setVolumeSize(2);

//attachRequest.withVolumeId(ebs.getSnapshotId());

AttachVolumeResult result=ec2.attachVolume(attachRequest);

任何帮助都非常感谢。提前谢谢

1 个答案:

答案 0 :(得分:3)

原因

EbsBlockDevice中的AWS SDK for Java类用于不同的目的,因此方法getSnapshotId()仅返回将从中创建卷的快照的ID ,即不是卷ID,因此是相应的例外。

解决方案

您最有可能想要使用课程CreateVolumeRequest,例如(从我的头脑中):

CreateVolumeRequest createVolumeRequest = new CreateVolumeRequest()
    .withAvailabilityZone("my instance's AZ") // The AZ in which to create the volume.
    .withSize(2); // The size of the volume, in gigabytes.

CreateVolumeResult createVolumeResult = ec2.createVolume(createVolumeRequest);

AttachVolumeRequest attachRequest = new AttachVolumeRequest()
    .withInstanceId("my instance id");
    .withVolumeId(createVolumeResult.getVolume().getVolumeId());

AttachVolumeResult attachResult = ec2.attachVolume(attachRequest);