Reputation: 11788
I am trying to find setting/method through which we can tell AWS SDK to delete an EBS volume on termination. This setting is available through AWS console but not through AWS Java SDK.
My code to create an EBS volume looks like below
CreateVolumeRequest volumeRequest = new CreateVolumeRequest(volumeSize, availabilityZone);
Volume vol = EC2_CLIENT.createVolume(volumeRequest).getVolume();
Can anyone please tell me how to enable delete on termination for EBS volume using Java SDK?
Upvotes: 2
Views: 2016
Reputation: 116
Probably you found the solution or workaround by yourself, but I had the same problem and I found this question on Google, so I'll write an answer for future developers.
It seems that enabling delete on termination cannot be done during creating or attaching an EBS, but only via modify instance attribute operation. This answer explains how to do this via CLI. Analogous method for AWS SDK for Java is to change device mapping settings by using AmazonEC2Client#modifyInstanceAttribute()
My code looks like this:
EbsInstanceBlockDeviceSpecification ebsSpecification = new EbsInstanceBlockDeviceSpecification()
.withDeleteOnTermination(true);
InstanceBlockDeviceMappingSpecification mappingSpecification = new InstanceBlockDeviceMappingSpecification()
.withDeviceName(deviceName)
.withEbs(ebsSpecification);
ModifyInstanceAttributeRequest request = new ModifyInstanceAttributeRequest()
.withInstanceId(instanceId)
.withBlockDeviceMappings(mappingSpecification);
ec2Client.modifyInstanceAttribute(request);
Upvotes: 3