Preserve data when an instance is terminated - Amazon Elastic Compute Cloud

Preserve data when an instance is terminated

Depending on your use case, you might want to preserve the data on your instance store volume or Amazon EBS volume when the Amazon EC2 instance is terminated. The data on an instance store volume doesn't persist when an instance is terminated. If you need to preserve the data stored on an instance store volume beyond the lifetime of the instance, you must manually copy that data to more persistent storage, such as an Amazon EBS volume, an Amazon S3 bucket, or an Amazon EFS file system. For more information, see Storage options for your Amazon EC2 instances.

For data on Amazon EBS volumes, Amazon EC2 uses the value of the DeleteOnTermination attribute for each attached Amazon EBS volume to determine whether to preserve or delete the volume.

The default value for the DeleteOnTermination attribute differs depending on whether the volume is the root volume of the instance or a non-root volume attached to the instance.

Root volume

By default, when you launch an instance the DeleteOnTermination attribute for the root volume of an instance is set to true. Therefore, the default is to delete the root volume of the instance when the instance terminates.

Non-root volume

By default, when you attach a non-root EBS volume to an instance, its DeleteOnTermination attribute is set to false. Therefore, the default is to preserve these volumes.

Note

After the instance terminates, you can take a snapshot of the preserved volume or attach it to another instance. You must delete a volume to avoid incurring further charges.

The DeleteOnTermination attribute can be set by the creator of an AMI as well as by the person who launches an instance. When the attribute is changed by the creator of an AMI or by the person who launches an instance, the new setting overrides the original AMI default setting. We recommend that you verify the default setting for the DeleteOnTermination attribute after you launch an instance with an AMI.

To verify if an Amazon EBS volume will be deleted on instance termination, view the details for volume on the instance's details pane. On the Storage tab, under Block devices, scroll right to view the Delete on termination setting for the volume.

  • If Yes, the volume will be deleted when the instance is terminated.

  • If No, the volume will not be deleted when the instance is terminated. Any volumes that are not deleted upon termination of the instance will continue to incur charges.

Change the root volume to persist at launch

Using the console, you can change the DeleteOnTermination attribute when you launch an instance. To change this attribute for a running instance, you must use the command line.

Use one of the following methods to change the root volume to persist at launch.

Console
To change the root volume of an instance to persist at launch using the console
  1. Follow the procedure to launch an instance, but don't launch the instance until you've completed the following steps to change the root volume to persist.

  2. Under Storage (volumes), expand the information under the root volume.

  3. For Delete on termination, choose No

  4. In the Summary panel, review your instance configuration, and then choose Launch instance. For more information, see Launch an EC2 instance using the launch instance wizard in the console.

Command line
To change the root volume of an instance to persist at launch using the commmand line

When you launch an EBS-backed instance, you can use one of the following commands to change the root device volume to persist. For more information about these command line interfaces, see Access Amazon EC2.

In the block device mappings for the volumes that you want to persist, include --DeleteOnTermination, and specify false.

For example, to persist a volume add the following option to your run-instances command:

--block-device-mappings file://mapping.json

In mapping.json, specify the device name, for example /dev/sda1 or /dev/xvda, and for --DeleteOnTermination, specify false.

[ { "DeviceName": "device_name", "Ebs": { "DeleteOnTermination": false } } ]

Change the root volume of a running instance to persist

You can use one of the following commands to change the root device volume of a running EBS-backed instance to persist. For more information about these command line interfaces, see Access Amazon EC2.

For example, use the following command:

aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --block-device-mappings file://mapping.json

In mapping.json, specify the device name, for example /dev/sda1 or /dev/xvda, and for --DeleteOnTermination, specify false.

[ { "DeviceName": "device_name", "Ebs": { "DeleteOnTermination": false } } ]