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.

madth3
  • 7,275
  • 12
  • 50
  • 74
Dan
  • 651
  • 2
  • 8
  • 19
  • 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 '11 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. – Chris Johnson Sep 18 '12 at 21:23

4 Answers4

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

Doron
  • 3,176
  • 7
  • 35
  • 60
  • 3
    Excellent answer. Googling on this issue finds surprisingly little documentation as to how to determine or modify this flag. –  Feb 05 '15 at 21:41
  • This was amazing. I was doing this by getting the attached volume through tagging filter. – mohit May 23 '17 at 08:56
  • Delete on termination was covered by the chapter [Preserve Amazon EBS volumes on instance termination](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/terminating-instances.html#preserving-volumes-on-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 '22 at 08:21
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}'
w00t
  • 616
  • 3
  • 9
  • 16
2

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

FigmentEngine
  • 511
  • 3
  • 8
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
      }
    }
]
Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45