简体   繁体   中英

How can attach new EBS volume to existing EC2 instance using java sdk?

I have tried using AttachVolumeRequest but in response i get following error

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

here is my code , in this code ec2 is my amazonclient object and its work fine so far

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

any help is highly appreciated. thanks in advance

Cause

Class EbsBlockDevice from the AWS SDK for Java serves a different purpose, accordingly method getSnapshotId() only returns The ID of the snapshot from which the volume will be created , ie not the volume ID, hence the respective exception.

Solution

You most likely want to use class CreateVolumeRequest instead, eg (from the top of my head):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM