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

sit_et 发布于 2019-03-09 java 最后更新 2019-03-09 14:34 3 浏览

我尝试过使用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);
任何帮助,高度赞赏。提前致谢
已邀请:

frerum

赞同来自:

原因 来自AWS SDK for JavaEbsBlockDevice类用于不同的目的,因此方法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);