fix(ec2): honor block device delete-on-termination updates (#9422)

ModifyInstanceAttribute accepted BlockDeviceMapping entries without changing the attached volume's DeleteOnTermination flag, and DescribeInstanceAttribute returned no mappings. Each requested mapping is now resolved to an attached volume by device name or volume ID and checked before any flag changes, an omitted DeleteOnTermination defaults to true as the API reference says, and DescribeInstanceAttribute returns the instance's block device mappings.

Closes #9384
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-25 06:52:10 -04:00
1 parent 87ff8135a9
commit b57af59bf8
4 files changed
+252 -2

No files matched your search

@@ -1700,6 +1700,9 @@ public class Ec2QueryHandler {
.end("item");
}
xml.end("groupSet");
} else if ("blockDeviceMapping".equals(attribute)) {
instanceBlockDeviceMappingXml(xml, service.instanceVolumeAttachments(List.of(inst))
.getOrDefault(instanceId, List.of()));
}
xml.end("DescribeInstanceAttributeResponse");
return xmlResponse(xml.build());
@@ -1732,9 +1735,57 @@ public class Ec2QueryHandler {
}
groupIds.add(groupId);
}
if (dryRun(p)) {
List<Ec2Service.InstanceBlockDeviceMappingChange> blockDeviceMappings = new ArrayList<>();
SortedSet<Integer> mappingIndices = new TreeSet<>();
for (String key : p.keySet()) {
if (!key.startsWith("BlockDeviceMapping.")) {
continue;
}
String[] parts = key.split("\\.", 3);
if (parts.length != 3) {
throw new AwsException("InvalidParameterValue", "Invalid block device mapping parameter: " + key,
400);
}
int index;
try {
index = Integer.parseInt(parts[1]);
} catch (NumberFormatException exception) {
throw new AwsException("InvalidParameterValue", "Invalid block device mapping index.", 400);
}
if (index < 1 || !Set.of("DeviceName", "Ebs.VolumeId",
"Ebs.DeleteOnTermination").contains(parts[2])) {
throw new AwsException("InvalidParameterValue", "Unsupported block device mapping parameter: " + key,
400);
}
mappingIndices.add(index);
}
for (int i : mappingIndices) {
String prefix = "BlockDeviceMapping." + i;
String deviceName = p.getFirst(prefix + ".DeviceName");
String volumeId = p.getFirst(prefix + ".Ebs.VolumeId");
String deleteOnTermination = p.getFirst(prefix + ".Ebs.DeleteOnTermination");
if ((deviceName != null && deviceName.isBlank()) || (volumeId != null && volumeId.isBlank())
|| (deviceName == null && volumeId == null)) {
throw new AwsException("InvalidParameterValue",
prefix + " must identify an attached device or volume.", 400);
}
Boolean value = parseOptionalBoolean(deleteOnTermination, prefix + ".Ebs.DeleteOnTermination");
blockDeviceMappings.add(new Ec2Service.InstanceBlockDeviceMappingChange(
deviceName, volumeId, value == null || value));
}
if (!blockDeviceMappings.isEmpty() && (userDataEncoded != null || attrName != null || !groupIds.isEmpty())) {
throw new AwsException("InvalidParameterCombination", "You can specify only one attribute at a time.",
400);
}
boolean dryRun = dryRun(p);
if (dryRun || !blockDeviceMappings.isEmpty()) {
service.checkInstanceAttributeChange(region, instanceId, userDataEncoded != null, attrName, groupIds);
throw Ec2Service.dryRunOperation();
if (!blockDeviceMappings.isEmpty()) {
service.checkInstanceBlockDeviceMappings(region, instanceId, blockDeviceMappings);
}
if (dryRun) {
throw Ec2Service.dryRunOperation();
}
}
if (userDataEncoded != null) {
service.modifyInstanceUserData(region, instanceId, userData, userDataEncoded);
@@ -1745,6 +1796,9 @@ public class Ec2QueryHandler {
if (!groupIds.isEmpty()) {
service.modifyInstanceGroups(region, instanceId, groupIds);
}
if (!blockDeviceMappings.isEmpty()) {
service.modifyInstanceBlockDeviceMappings(region, instanceId, blockDeviceMappings);
}
return booleanResponse("ModifyInstanceAttribute");
}
@@ -6432,6 +6432,59 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
storeInstance(region, inst);
}
public record InstanceBlockDeviceMappingChange(String deviceName, String volumeId, boolean deleteOnTermination) {}
public void checkInstanceBlockDeviceMappings(String region, String instanceId,
List<InstanceBlockDeviceMappingChange> changes) {
resolveInstanceBlockDeviceMappings(region, instanceId, changes);
}
public void modifyInstanceBlockDeviceMappings(String region, String instanceId,
List<InstanceBlockDeviceMappingChange> changes) {
Map<String, InstanceBlockDeviceMappingChange> byVolume =
resolveInstanceBlockDeviceMappings(region, instanceId, changes);
for (Map.Entry<String, InstanceBlockDeviceMappingChange> entry : byVolume.entrySet()) {
String volumeId = entry.getKey();
InstanceBlockDeviceMappingChange change = entry.getValue();
withVolumeLock(volumeId, () -> {
Volume volume = getRequiredVolume(region, volumeId);
VolumeAttachment attachment = volume.getAttachments().stream()
.filter(candidate -> instanceId.equals(candidate.getInstanceId())
&& (change.deviceName() == null
|| isSameDevice(change.deviceName(), candidate.getDevice())))
.findFirst()
.orElseThrow(() -> new AwsException("InvalidParameterValue",
"The volume '" + volumeId + "' is not attached to instance '"
+ instanceId + "'.", 400));
attachment.setDeleteOnTermination(change.deleteOnTermination());
volumes.put(key(region, volumeId), volume);
});
}
}
private Map<String, InstanceBlockDeviceMappingChange> resolveInstanceBlockDeviceMappings(
String region, String instanceId, List<InstanceBlockDeviceMappingChange> changes) {
ensureDefaultResources(region);
Instance inst = requireEc2ManagedInstance(region, instanceId, "modified");
List<VolumeAttachment> attachments = instanceVolumeAttachments(List.of(inst))
.getOrDefault(instanceId, List.of());
Map<String, InstanceBlockDeviceMappingChange> byVolume = new LinkedHashMap<>();
for (InstanceBlockDeviceMappingChange change : changes) {
VolumeAttachment attachment = attachments.stream()
.filter(candidate -> (change.deviceName() == null
|| isSameDevice(change.deviceName(), candidate.getDevice()))
&& (change.volumeId() == null || change.volumeId().equals(candidate.getVolumeId())))
.findFirst()
.orElseThrow(() -> new AwsException("InvalidParameterValue",
"The block device mapping is not attached to instance '" + instanceId + "'.", 400));
if (byVolume.putIfAbsent(attachment.getVolumeId(), change) != null) {
throw new AwsException("InvalidParameterValue",
"The volume '" + attachment.getVolumeId() + "' is specified more than once.", 400);
}
}
return byVolume;
}
/**
* Replaces the security groups attached to an instance (ModifyInstanceAttribute with
* {@code GroupId.N}). Validates each group, updates the instance and its network interfaces,