13

I have an EC2 instance that I'd like to take a snapshot of, to use as an AMI for future spot instances. Because of the way I created volume for this instance, it is currently set to not delete upon termination.
I want it to delete on termination, so that I can use it for spot instances and not have residual volumes hanging around needing manual deletion.

I've combed AWS manual, stack exchange, google, etc and I can only find references to a 'delete on termination' flag, but no explanation of how to use it.

2
  • 2
    I got the answer, but stackoverflow won't let me submit it for a while because I'm too much a newb. The trick is to use the argumennt -b "/dev/sda1=::true" after ec2-request-spot-instances
    – Dan
    Nov 15, 2011 at 19:08
  • The same -b option can be used on ec2-modify-instance-attributes to configure an instance that has already been placed into service. Sep 18, 2012 at 21:23

4 Answers 4

10

Taking on what @akshar wrote, you can have it all in the same line, without the need for an additional json file:

 aws ec2 modify-instance-attribute --instance-id i-123abc45 --block-device-mappings "[{\"DeviceName\": \"/dev/sdf\",\"Ebs\":{\"DeleteOnTermination\":true}}]"

where /dev/sdf is the mount point in your instance

3
  • 3
    Excellent answer. Googling on this issue finds surprisingly little documentation as to how to determine or modify this flag.
    – user1832464
    Feb 5, 2015 at 21:41
  • This was amazing. I was doing this by getting the attached volume through tagging filter.
    – mohit
    May 23, 2017 at 8:56
  • Delete on termination was covered by the chapter Preserve Amazon EBS volumes on instance termination of the official EC2 guide. It only offers the choice of CLI as Doron has described, it cannot be done through the console.
    – Corral
    Jul 26, 2022 at 8:21
2

enable delete on termination, for example http://itsecureadmin.com/2011/06/aws-instance-ebs-volume-delete-on-termination/

2

Taking on what everybody else said, one line and without JSON encoding and ugly escapes:

modify-instance-attribute --instance-id $ID --block-device-mappings 'DeviceName=/dev/sdf,Ebs={DeleteOnTermination=true}'
1

You can use AWS-CLI to do this:

The simplest way is to use modify-instance-attribute subcommand provided by aws ec2 command.

aws ec2 modify-instance-attribute --instance-id i-123ab12f --block-device-mappings file://~/some.json 

Content of file some.json should be:

[
    {
    "DeviceName": "/dev/sda1",
    "Ebs": {
      "DeleteOnTermination": true
      }
    }
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.