EC2 volume: how do I set it so that it WILL delete on termination?
Asked Answered
S

4

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.

Sapphirine answered 15/11, 2011 at 15:9 Comment(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-instancesSapphirine
The same -b option can be used on ec2-modify-instance-attributes to configure an instance that has already been placed into service.Trenton
R
2

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

Reforest answered 15/11, 2011 at 20:25 Comment(0)
B
12

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

Benzol answered 5/7, 2014 at 13:13 Comment(3)
Excellent answer. Googling on this issue finds surprisingly little documentation as to how to determine or modify this flag.Northnortheast
This was amazing. I was doing this by getting the attached volume through tagging filter.Eudoca
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.Slut
R
2

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

Reforest answered 15/11, 2011 at 20:25 Comment(0)
H
2

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
      }
    }
]

Shameless Plug: If you regularly work with AWS, take this highly engaging AWS Quiz to check your AWS knowledge.

Heaven answered 24/2, 2014 at 15:12 Comment(0)
P
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}'
Puff answered 5/4, 2018 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.