

# Terminate Amazon EC2 instances
<a name="terminating-instances"></a>

**Warning**  
**Terminating an instance is permanent and irreversible.**  
After you terminate an instance, you can no longer connect to it, and it can't be recovered. All attached Amazon EBS volumes that are configured to be deleted on termination are also permanently deleted and can't be recovered. All data stored on instance store volumes is permanently lost. For more information, see [How instance termination works](how-ec2-instance-termination-works.md).  
Before you terminate an instance, ensure that you have backed up all data that you need to retain after the termination to persistent storage.

You can delete your instance when you no longer need it. This is referred to as *terminating* your instance. As soon as the state of an instance changes to `shutting-down` or `terminated`, you stop incurring charges for that instance.

You can't connect to or start an instance after you've terminated it. However, you can launch new instances using the same AMI.

If you'd rather stop or hibernate your instance, see [Stop and start Amazon EC2 instances](Stop_Start.md) or [Hibernate your Amazon EC2 instance](Hibernate.md). For more information, see [Differences between instance states](ec2-instance-lifecycle.md#lifecycle-differences).

**Topics**
+ [How instance termination works](how-ec2-instance-termination-works.md)
+ [Methods for terminating an instance](instance-terminate-methods.md)
+ [Terminate an instance with a graceful OS shutdown](#terminating-instances-console)
+ [Terminate an instance and bypass the graceful OS shutdown](#terminating-instances-bypass-graceful-os-shutdown)
+ [Troubleshoot instance termination](#troubleshoot-instance-terminate)
+ [Change instance termination protection](Using_ChangingDisableAPITermination.md)
+ [Change instance initiated shutdown behavior](Using_ChangingInstanceInitiatedShutdownBehavior.md)
+ [Preserve data when an instance is terminated](preserving-volumes-on-termination.md)

# How instance termination works
<a name="how-ec2-instance-termination-works"></a>

When you terminate an instance, changes are registered at the operating system (OS) level of the instance, some resources are lost, and some resources persist.

The following diagram shows what is lost and what persists when an Amazon EC2 instance is terminated. When an instance terminates, the data on any instance store volumes and the data stored in the instance RAM is erased. Any Elastic IP addresses associated with the instance are detached. For Amazon EBS root volumes and data volumes, the outcome depends on the **Delete on termination** setting of each volume.

![\[The IP addresses, RAM, instance store volumes, and EBS root volume are lost when an instance is terminated.\]](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/images/terminate-instance.png)


## Considerations
<a name="terminate-instance-overview"></a>
+ **Data persistence**
  + Instance store volumes: All data is permanently deleted when the instance terminates. 
  + EBS root volume:
    + When attached at launch, deleted by default when the instance terminates.
    + When attached after launch, persists by default when the instance terminates.
  + EBS data volumes:
    + When attached at launch using the console: Persists by default when the instance terminates.
    + When attached at launch using the CLI: Deleted by default when the instance terminates.
    + When attached after launch using the console or CLI: Persists by default when the instance terminates.
**Note**  
Any volumes that are not deleted on instance termination continue to incur charges. You can change the setting so that a volume is deleted or persists on instance termination. For more information, see [Preserve data when an instance is terminated](preserving-volumes-on-termination.md).
+ **Protection against accidental termination**
  + To prevent an instance from being accidentally terminated by someone, [enable termination protection](Using_ChangingDisableAPITermination.md).
  + To control whether an instance stops or terminates when shutdown is initiated from the instance, change the [instance initiated shutdown behavior](Using_ChangingInstanceInitiatedShutdownBehavior.md).
+ **Shutdown scripts** – If you run a script on instance termination, your instance might have an abnormal termination because we have no way of ensuring that shutdown scripts run. Amazon EC2 attempts to cleanly shut down an instance and run any system shutdown scripts; however, certain events (such as hardware failure) may prevent these system shutdown scripts from running.
+ **Bare metal instances** – x86 bare metal instances don't support cooperative shutdown.

## What happens when you terminate an instance
<a name="what-happens-terminate"></a>

**Changes registered at the OS level**
+ The API request sends a button press event to the guest.
+ Various system services are stopped as a result of the button press event. Graceful shutdown of the system is provided by **systemd** (Linux) or the System process (Windows). Graceful shutdown is triggered by the ACPI shutdown button press event from the hypervisor.
+ ACPI shutdown is initiated.
+ The instance shuts down after the graceful shutdown process exits. There is no configurable OS shutdown time. The instance remains visible in the console for a short time, then the entry is automatically deleted.

**Resources lost**
+ Data stored on the instance store volumes.
+ EBS root volume if the `DeleteOnTermination` attribute is set to `true`.
+ EBS data volumes (attached at launch or after) if the `DeleteOnTermination` attribute is set to `true`.

**Resources that persist**
+ EBS root volume if the `DeleteOnTermination` attribute is set to `false`.
+ EBS data volumes (attached at launch or after) if the `DeleteOnTermination` attribute is set to `false`.

## Test application response to instance termination
<a name="test-terminate-instance"></a>

You can use AWS Fault Injection Service to test how your application responds when your instance is terminated. For more information, see the [AWS Fault Injection Service User Guide](https://docs.aws.amazon.com/fis/latest/userguide/what-is.html).

# Methods for terminating an instance
<a name="instance-terminate-methods"></a>

**Warning**  
**Terminating an instance is permanent and irreversible.**  
After you terminate an instance, you can no longer connect to it, and it can't be recovered. All attached Amazon EBS volumes that are configured to be deleted on termination are also permanently deleted and can't be recovered. All data stored on instance store volumes is permanently lost. For more information, see [How instance termination works](how-ec2-instance-termination-works.md).  
Before you terminate an instance, ensure that you have backed up all data that you need to retain after the termination to persistent storage.

There are four ways to perform a user-initiated instance termination: default terminate, terminate with skip OS shutdown, force terminate, and force terminate with skip OS shutdown. The following table compares the key differences between the termination methods:

**Note**  
You can't terminate an instance if termination protection is turned on. For more information, see [Change instance termination protection](Using_ChangingDisableAPITermination.md).


| Termination method | Key purpose | Use case | CLI command | 
| --- | --- | --- | --- | 
| Default terminate | Normal instance shutdown with attempted graceful OS shutdown. | Typical instance termination. | <pre>aws ec2 terminate-instances \<br />--instance-id i-1234567890abcdef0</pre> | 
| Terminate with skip OS shutdown | Bypasses the graceful OS shutdown when terminating an instance. | When bypassing graceful OS shutdown is required. | <pre>aws ec2 terminate-instances \<br />--instance-id i-1234567890abcdef0 \<br />--skip-os-shutdown</pre> | 
| Force terminate | Handles stuck instances. Attempts a default termination first; if instance fails to terminate, then forcibly terminates the instance. | When instance is stuck in shutting-down state. | <pre>aws ec2 terminate-instances \<br />--instance-id i-1234567890abcdef0 \<br />--force</pre> | 
| Force terminate with skip OS shutdown | Force terminates and bypasses the graceful OS shutdown when terminating an instance. | When force termination and bypassing graceful OS shutdown is required. | <pre>aws ec2 terminate-instances \<br />--instance-id i-1234567890abcdef0 \<br />--force \<br />--skip-os-shutdown</pre> | 

For instructions on how to use each method, see the following:
+ [Terminate an instance with a graceful OS shutdown](terminating-instances.md#terminating-instances-console)
+ [Terminate an instance and bypass the graceful OS shutdown](terminating-instances.md#terminating-instances-bypass-graceful-os-shutdown)
+ [Force terminate an instance](TroubleshootingInstancesShuttingDown.md#force-terminate-ec2-instance)

## Terminate an instance with a graceful OS shutdown
<a name="terminating-instances-console"></a>

You can terminate an instance using the default terminate method, which includes an attempt at a graceful OS shutdown. For more information, see [Methods for terminating an instance](instance-terminate-methods.md).

------
#### [ Console ]

**To terminate an instance using the default terminate method**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. In the navigation pane, choose **Instances**.

1. Select the instance, and choose **Instance state**, **Terminate (delete) instance**.

1. Choose **Terminate (delete)** when prompted for confirmation.

1. After you terminate an instance, it remains visible for a short while, with a state of `terminated`.

   If termination fails or if a terminated instance is visible for more than a few hours, see [Terminated instance still displayed](TroubleshootingInstancesShuttingDown.md#terminated-instance-still-displaying).

------
#### [ AWS CLI ]

**To terminate an instance using the default terminate method**  
Use the [terminate-instances](https://docs.aws.amazon.com/cli/latest/reference/ec2/terminate-instances.html) command.

```
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
```

------
#### [ PowerShell ]

**To terminate an instance using the default terminate method**  
Use the [Remove-EC2Instance](https://docs.aws.amazon.com/powershell/latest/reference/items/Remove-EC2Instance.html) cmdlet.

```
Remove-EC2Instance -InstanceId i-1234567890abcdef0
```

------

## Terminate an instance and bypass the graceful OS shutdown
<a name="terminating-instances-bypass-graceful-os-shutdown"></a>

You can bypass the graceful OS shutdown when terminating an instance. For more information, see [Methods for terminating an instance](instance-terminate-methods.md).

------
#### [ Console ]

**To terminate an instance and bypass the graceful OS shutdown**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. In the navigation pane, choose **Instances**.

1. Select the instance, and choose **Instance state**, **Terminate (delete) instance**.

1. Under **Skip OS shutdown**, select the **Skip OS shutdown** checkbox. If you don't see this option in the console, it's not yet available in the console in the current Region. You can, however, access this feature using the AWS CLI or SDK, or try another Region in the console.

1. Choose **Terminate (delete)**.

1. After you terminate an instance, it remains visible for a short while, with a state of `terminated`.

   If termination fails or if a terminated instance is visible for more than a few hours, see [Terminated instance still displayed](TroubleshootingInstancesShuttingDown.md#terminated-instance-still-displaying).

------
#### [ AWS CLI ]

**To terminate an instance and bypass the graceful OS shutdown**  
Use the [terminate-instances](https://docs.aws.amazon.com/cli/latest/reference/ec2/terminate-instances.html) command with `--skip-os-shutdown`..

```
aws ec2 terminate-instances \
    --instance-ids i-1234567890abcdef0 \
    --skip-os-shutdown
```

------
#### [ PowerShell ]

**To terminate an instance and bypass the graceful OS shutdown**  
Use the [Remove-EC2Instance](https://docs.aws.amazon.com/powershell/latest/reference/items/Remove-EC2Instance.html) cmdlet with `-SkipOsShutdown $true`..

```
Remove-EC2Instance `
    -InstanceId i-1234567890abcdef0 `
    -SkipOsShutdown $true
```

------

## Troubleshoot instance termination
<a name="troubleshoot-instance-terminate"></a>

The requester must have permission to call `ec2:TerminateInstances`. For more information, see [Example policies to work with instances](ExamplePolicies_EC2.md#iam-example-instances).

If you terminate your instance and another instance starts, most likely you have configured automatic scaling through a feature like EC2 Fleet or Amazon EC2 Auto Scaling. For more information, see [Instances automatically launched or terminated](TroubleshootingInstancesShuttingDown.md#automatic-instance-create-or-delete).

**Note**  
You can't terminate an instance if termination protection is turned on. For more information, see [Change instance termination protection](Using_ChangingDisableAPITermination.md).

If your instance is in the `shutting-down` state for longer than usual, you can attempt to force terminate it. If it remains in the `shutting-down` state, it should be cleaned up (terminated) by automated processes within the Amazon EC2 service. For more information, see [Delayed instance termination](TroubleshootingInstancesShuttingDown.md#instance-stuck-terminating).

# Change instance termination protection
<a name="Using_ChangingDisableAPITermination"></a>

To prevent your instance from being accidentally terminated using the Amazon EC2 API, whether you call `TerminateInstances` directly or using another interface such as the Amazon EC2 console, enable *termination protection* for the instance. The `DisableApiTermination` attribute controls whether the instance can be terminated. By default, termination protection is disabled for your instance. You can set the value of this attribute when you launch an instance, or while the instance is running or stopped.

The `DisableApiTermination` attribute doesn't prevent you from terminating an instance by initiating shutdown from the instance (for example, by using an operating system command for system shutdown) when the `InstanceInitiatedShutdownBehavior` attribute is set to `terminate`. For more information, see [Change instance initiated shutdown behavior](Using_ChangingInstanceInitiatedShutdownBehavior.md).

**Considerations**
+ Enabling termination protection does not prevent AWS from terminating the instance when there is a [scheduled event](monitoring-instances-status-check_sched.md) to terminate the instance.
+ Enabling termination protection does not prevent Amazon EC2 Auto Scaling from terminating an instance when the instance is unhealthy or during scale-in events. You can control whether an Auto Scaling group can terminate a particular instance when scaling using [instance scale-in protection](https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-instance-protection.html). You can control whether an Auto Scaling group can terminate unhealthy instances by [suspending the ReplaceUnhealthy scaling process](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-suspend-resume-processes.html).
+ You can't enable termination protection for Spot Instances.

------
#### [ Console ]

**To enable termination protection for an instance at launch**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. On the dashboard, choose **Launch instance**.

1. Expand **Advanced details**. For **Termination protection**, select **Enable**.

1. When you are finishing specifying the details for your instance, choose **Launch instance**.

**To change termination protection for an instance**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. In the navigation pane, select **Instances**.

1. Select the instance.

1. Choose **Actions**, **Instance settings**, **Change termination protection**.

1. For **Termination protection** select or clear **Enable**.

1. Choose **Save**.

------
#### [ AWS CLI ]

**To enable termination protection for an instance**  
Use the [modify-instance-attribute](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-attribute.html) command.

```
aws ec2 modify-instance-attribute \
    --instance-id i-1234567890abcdef0 \
    --disable-api-termination
```

**To disable termination protection for an instance**  
Use the [modify-instance-attribute](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-attribute.html) command.

```
aws ec2 modify-instance-attribute \
    --instance-id i-1234567890abcdef0 \
    --no-disable-api-termination
```

------
#### [ PowerShell ]

**To enable termination protection for an instance**  
Use the [Edit-EC2InstanceAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceAttribute.html) cmdlet.

```
Edit-EC2InstanceAttribute `
    -InstanceId i-1234567890abcdef0 `
    -DisableApiTermination $true
```

**To disable termination protection for an instance**  
Use the [Edit-EC2InstanceAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceAttribute.html) cmdlet.

```
Edit-EC2InstanceAttribute `
    -InstanceId i-1234567890abcdef0 `
    -DisableApiTermination $false
```

------

## Terminate multiple instances with termination protection
<a name="terminate-multiple"></a>

If you terminate multiple instances across multiple Availability Zones in the same request, and one or more of the specified instances are enabled for termination protection, the request fails with the following results:
+ The specified instances that are in the same Availability Zone as the protected instance are not terminated.
+ The specified instances that are in different Availability Zones, where no other specified instances are protected, are successfully terminated.

**Example**  
Suppose that you have the following four instances across two Availability Zones.

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_ChangingDisableAPITermination.html)

If you attempt to terminate all of these instances in the same request, the request reports failure with the following results:
+ **Instance 1** and **Instance 2** are successfully terminated because neither instance is enabled for termination protection.
+ **Instance 3** and **Instance 4** fail to terminate because **Instance 3** is enabled for termination protection.

# Change instance initiated shutdown behavior
<a name="Using_ChangingInstanceInitiatedShutdownBehavior"></a>

**Warning**  
**Terminating an instance is permanent and irreversible.**  
After you terminate an instance, you can no longer connect to it, and it can't be recovered. All attached Amazon EBS volumes that are configured to be deleted on termination are also permanently deleted and can't be recovered. All data stored on instance store volumes is permanently lost. For more information, see [How instance termination works](how-ec2-instance-termination-works.md).  
Before you terminate an instance, ensure that you have backed up all data that you need to retain after the termination to persistent storage.

By default, when you initiate a shutdown from an Amazon EBS backed instance (using a command such as **shutdown** or **poweroff**), the instance stops. You can change this behavior so that the instance terminates instead by changing the `InstanceInitiatedShutdownBehavior` attribute for the instance. You can change this attribute while the instance is running or stopped.

The **halt** command doesn't initiate a shutdown. If used, the instance doesn't terminate; instead, it places the CPU into `HLT` and the instance continues to run.

**Note**  
The `InstanceInitiatedShutdownBehavior` attribute only applies when you perform a shutdown from the operating system of the instance itself. It doesn't apply when you stop an instance using the `StopInstances` API or the Amazon EC2 console.

------
#### [ Console ]

**To change the instance initiated shutdown behavior**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. In the navigation pane, choose **Instances**.

1. Select the instance.

1. Choose **Actions**, **Instance settings**, **Change shutdown behavior**.

   **Shutdown behavior** displays the current behavior.

1. To change the behavior, for **Shutdown behavior**, choose **Stop** or **Terminate**. 

1. Choose **Save**.

------
#### [ AWS CLI ]

**To change the instance initiated shutdown behavior**  
Use the [modify-instance-attribute](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-attribute.html) command.

```
aws ec2 modify-instance-attribute \
    --instance-id i-1234567890abcdef0 \
    --instance-initiated-shutdown-behavior terminate
```

------
#### [ PowerShell ]

**To change the instance initiated shutdown behavior**  
Use the [Edit-EC2InstanceAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceAttribute.html) cmdlet.

```
Edit-EC2InstanceAttribute `
    -InstanceId i-1234567890abcdef0 `
    -InstanceInitiatedShutdownBehavior terminate
```

------

# Preserve data when an instance is terminated
<a name="preserving-volumes-on-termination"></a>

When an Amazon EC2 instance is terminated, you can preserve the data on your instance store volumes or Amazon EBS volumes. This topic explains how to ensure your data persists beyond instance termination.

## How instance termination affects root and data volumes
<a name="how-instance-termination-affects-root-and-data-volumes"></a>

**Instance store volumes**  
When an instance is terminated, the instance store volumes are automatically deleted and the data is lost. To preserve this data beyond the lifetime of the instance, before terminating the instance, manually copy the data to 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](Storage.md).

**Amazon EBS volumes**  
When an instance is terminated, the EBS volumes are either deleted or preserved, depending on the value of the `DeleteOnTermination` attribute for each volume:
+ **Yes** (console) / `true` (CLI) – The volume is deleted when the instance is terminated.
+ **No** (console) / `false` (CLI) – The volume is preserved when the instance is terminated. Preserved volumes continue to incur charges.
**Note**  
After an instance terminates, you can take a snapshot of the preserved volume or attach it to another instance. To avoid incurring charges, you must delete the volume.

## Default deletion behavior for EBS volumes
<a name="default-deletion-behavior-for-ebs-volumes"></a>

The default `DeleteOnTermination` value differs depending on the volume type, whether the volume was attached at launch or after, and the method (console or CLI) used to attach the volume:


| Volume type | Attached when | Method for attaching | Default behavior on instance termination | 
| --- | --- | --- | --- | 
| Root volume | At launch | Console or CLI | Delete | 
| Root volume | After launch | Console or CLI | Preserve | 
| Data volume | At launch | Console | Preserve | 
| Data volume | At launch | CLI | Delete | 
| Data volume | After launch | Console and CLI | Preserve | 

## Check volume persistence settings
<a name="check-ebs-volume-persistence-settings"></a>

The default value at launch for an EBS volume is determined by the `DeleteOnTermination` attribute set on the AMI. You can change the value at instance launch, overriding the AMI setting. We recommend that you verify the default setting for the `DeleteOnTermination` attribute after you launch an instance.

**To check if an Amazon EBS volume will be deleted on instance termination**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. In the navigation pane, choose **Instances**.

1. Select the instance.

1. Choose the **Storage** tab.

1. Under **Block devices**, scroll right to check the **Delete on termination** column.
   + If **Yes**, the volume is deleted when the instance is terminated.
   + If **No**, the volume is not be deleted when the instance is terminated. Any volumes not deleted continue to incur charges.

## Change the root volume to persist at launch
<a name="delete-on-termination-ebs-volume"></a>

You can change the `DeleteOnTermination` attribute of an EBS root volume when you launch an instance. You can also use the following procedure for a data volume.

------
#### [ Console ]

**To change the root volume of an instance to persist at launch**

1. Follow the procedure to [launch an instance](ec2-launch-instance-wizard.md), but don't launch the instance until you've completed the following steps to change the root volume to persist.

1. On the **Configure storage** pane, choose **Advanced**.

1. Under **EBS volumes**, expand the root volume information.

1. For **Delete on termination**, choose **No**.

1. 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](ec2-launch-instance-wizard.md).

------
#### [ AWS CLI ]

**To change the root volume of an instance to persist at launch**  
Use the [run-instances](https://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html) command to change the value of `DeleteOnTermination` in the block device mapping.

Add the `--block-device-mappings` option:

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

------
#### [ PowerShell ]

**To change the root volume of an instance to persist at launch**  
Use the [New-EC2Instance](https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Instance.html) cmdlet to change the value of `DeleteOnTermination` in the block device mapping.

Add the `-BlockDeviceMapping` option:

```
-BlockDeviceMapping $bdm
```

In `bdm`, specify the device name, for example `/dev/sda1` or `/dev/xvda`, and for `DeleteOnTermination`, specify `false`.

```
$ebd = New-Object -TypeName Amazon.EC2.Model.EbsBlockDevice
$ebd.DeleteOnTermination = false
$bdm = New-Object -TypeName Amazon.EC2.Model.BlockDeviceMapping
$bdm.DeviceName = "/dev/sda1"
$bdm.Ebs = $ebd
```

------

## Change the root volume of a running instance to persist
<a name="delete-on-termination-running-instance"></a>

You can change the EBS root volume of a running instance to persist. You can also use the following procedure for a data volume.

------
#### [ AWS CLI ]

**To change the root volume to persist**  
Use the [modify-instance-attribute](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-attribute.html) 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
    }
  }
]
```

------
#### [ PowerShell ]

**To change the root volume to persist**  
Use the [Edit-EC2InstanceAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceAttribute.html) cmdlet.

Add the `-BlockDeviceMapping` option:

```
-BlockDeviceMapping $bdm
```

In `bdm`, specify the device name, for example `/dev/sda1` or `/dev/xvda`, and for `DeleteOnTermination`, specify `false`.

```
$ebd = New-Object -TypeName Amazon.EC2.Model.EbsBlockDevice
$ebd.DeleteOnTermination = false
$bdm = New-Object -TypeName Amazon.EC2.Model.BlockDeviceMapping
$bdm.DeviceName = "/dev/sda1"
$bdm.Ebs = $ebd
```

------