Changing block-device-mapping for AWS AMI
As you know, every AWS AMI have it's own Block Device Mapping.
What if you want to modify it for some AMI? For example, you have all EBS volumes "delete-on-termination" flag set to "false" and you want to change it to "true", so you avoid having EBS volumes hanging after instance deletion.
Well, you can't modify existing AMI block device mapping. You need either to create new image (AMI) from instance or to modify block device mapping during instance launch.
Here is one code snippet with "jq" tool which you can use to describe current image, change it's "delete-on-termination" flag to true and register new image from the running instance (which was created originally from that image):
$aws ec2 describe-images --image-ids $ORIGINAL_AMI | jq '.Images[0] | {BlockDeviceMappings: [.BlockDeviceMappings[] | {DeviceName: .DeviceName, Ebs: .Ebs | {DeleteOnTermination: true}}]}' | sed '1d;$d'| sed -e "s/\"BlockDeviceMappings\"://g" > NEW_BLOCK_DEVICE_MAPPING.TXT
$aws register-image --instance-id INSTANCE-ID --block-device-mappings file://NEW_BLOCK_DEVICE_MAPPING.TXT --name "new-image-name" --description "Image description"