Add block device mappings to Amazon EC2 instance - Amazon Elastic Compute Cloud
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Add block device mappings to Amazon EC2 instance

By default, an instance that you launch includes any storage devices specified in the block device mapping of the AMI from which you launched the instance. You can specify changes to the block device mapping for an instance when you launch it, and these updates overwrite or merge with the block device mapping of the AMI.

Limitations
  • For the root volume, you can only modify the following: volume size, volume type, and the Delete on Termination flag.

  • When you modify an EBS volume, you can't decrease its size. Therefore, you must specify a snapshot whose size is equal to or greater than the size of the snapshot specified in the block device mapping of the AMI.

Update the block device mapping when launching an instance

You can add EBS volumes and instance store volumes to an instance when you launch it. Note that updating the block device mapping for an instance doesn't make a permanent change to the block device mapping of the AMI from which it was launched.

Console
To add volumes to an instance using the console
  1. Open the Amazon EC2 console.

  2. From the dashboard, choose Launch Instance.

  3. On the Choose an Amazon Machine Image (AMI) page, select the AMI to use and choose Select.

  4. Follow the wizard to complete the Choose an Instance Type and Configure Instance Details pages.

  5. On the Add Storage page, you can modify the root volume, EBS volumes, and instance store volumes as follows:

    • To change the size of the root volume, locate the Root volume under the Type column, and change its Size field.

    • To suppress an EBS volume specified by the block device mapping of the AMI used to launch the instance, locate the volume and click its Delete icon.

    • To add an EBS volume, choose Add New Volume, choose EBS from the Type list, and fill in the fields (Device, Snapshot, and so on).

    • To suppress an instance store volume specified by the block device mapping of the AMI used to launch the instance, locate the volume, and choose its Delete icon.

    • To add an instance store volume, choose Add New Volume, select Instance Store from the Type list, and select a device name from Device.

  6. Complete the remaining wizard pages, and choose Launch.

Command line
To add volumes to an instance using the Amazon CLI

Use the run-instances Amazon CLI command with the --block-device-mappings option to specify a block device mapping for an instance at launch.

For example, suppose that an EBS-backed AMI specifies the following block device mapping for a Linux instance:

  • /dev/sdb = ephemeral0

  • /dev/sdh = snap-1234567890abcdef0

  • /dev/sdj = 100

To prevent /dev/sdj from attaching to an instance launched from this AMI, use the following mapping.

{ "DeviceName": "/dev/sdj", "NoDevice": "" }

To increase the size of /dev/sdh to 300 GiB, specify the following mapping. Notice that you don't need to specify the snapshot ID for /dev/sdh, because specifying the device name is enough to identify the volume.

{ "DeviceName": "/dev/sdh", "Ebs": { "VolumeSize": 300 } }

To increase the size of the root volume at instance launch, first call describe-images with the ID of the AMI to verify the device name of the root volume. For example, "RootDeviceName": "/dev/xvda". To override the size of the root volume, specify the device name of the root device used by the AMI and the new volume size.

{ "DeviceName": "/dev/xvda", "Ebs": { "VolumeSize": 100 } }

To attach an additional instance store volume, /dev/sdc, specify the following mapping. If the instance type doesn't support multiple instance store volumes, this mapping has no effect. If the instance supports NVMe instance store volumes, they are automatically enumerated and assigned an NVMe device name.

{ "DeviceName": "/dev/sdc", "VirtualName": "ephemeral1" }
To add volumes to an instance using the Amazon Tools for Windows PowerShell

Use the -BlockDeviceMapping parameter with the New-EC2Instance command (Amazon Tools for Windows PowerShell).

Update the block device mapping of a running instance

You can use the modify-instance-attribute Amazon CLI command to update the block device mapping of a running instance. You do not need to stop the instance before changing this attribute.

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

For example, to preserve the root volume at instance termination, specify the following in mapping.json.

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

Alternatively, you can use the -BlockDeviceMapping parameter with the Edit-EC2InstanceAttribute command (Amazon Tools for Windows PowerShell).

View the EBS volumes in an instance block device mapping

You can easily enumerate the EBS volumes mapped to an instance.

Note

For instances launched before the release of the 2009-10-31 API, Amazon can't display the block device mapping. You must detach and reattach the volumes so that Amazon can display the block device mapping.

Console
To view the EBS volumes for an instance using the console
  1. Open the Amazon EC2 console.

  2. In the navigation pane, choose Instances.

  3. In the search box, enter Root device type, and then choose EBS. This displays a list of EBS-backed instances.

  4. Select the desired instance and look at the details displayed in the Storage tab. At a minimum, the following information is available for the root device:

    • Root device type (for example, EBS)

    • Root device name (for example, /dev/xvda)

    • Block devices (for example, /dev/xvda, /dev/sdf, and /dev/sdj)

    If the instance was launched with additional EBS volumes using a block device mapping, they appear under Block devices. Any instance store volumes do not appear on this tab.

  5. To display additional information about an EBS volume, choose its volume ID to go to the volume page.

Command line

To view the EBS volumes for an instance using the command line

Use the describe-instances (Amazon CLI) command or Get-EC2Instance (Amazon Tools for Windows PowerShell) command to enumerate the EBS volumes in the block device mapping for an instance.

View the instance block device mapping for instance store volumes

The instance type determines the number and type of instance store volumes that are available to the instance. If the number of instance store volumes in a block device mapping exceeds the number of instance store volumes available to an instance, the additional volumes are ignored. To view the instance store volumes for your instance, run the lsblk command (Linux instances) or open Windows Disk Management (Windows instances). To learn how many instance store volumes are supported by each instance type, see Amazon EC2 instance type specifications.

When you view the block device mapping for your instance, you can see only the EBS volumes, not the instance store volumes. The method you use to view the instance store volumes for your instance depends on the volume type.

NVMe instance store volumes

You can use the NVMe command line package, nvme-cli, to query the NVMe instance store volumes in the block device mapping. Download and install the package on your instance, and then run the following command.

[ec2-user ~]$ sudo nvme list

The following is example output for an instance. The text in the Model column indicates whether the volume is an EBS volume or an instance store volume. In this example, both /dev/nvme1n1 and /dev/nvme2n1 are instance store volumes.

Node SN Model Namespace ---------------- -------------------- ---------------------------------------- --------- /dev/nvme0n1 vol06afc3f8715b7a597 Amazon Elastic Block Store 1 /dev/nvme1n1 AWS2C1436F5159EB6614 Amazon EC2 NVMe Instance Storage 1 /dev/nvme2n1 AWSB1F4FF0C0A6C281EA Amazon EC2 NVMe Instance Storage 1 ...

You can use Disk Management or PowerShell to list both EBS and instance store NVMe volumes. For more information, see List NVMe volumes.

HDD or SSD instance store volumes

You can use instance metadata to query the HDD or SSD instance store volumes in the block device mapping. NVMe instance store volumes are not included.

The base URI for all requests for instance metadata is http://169.254.169.254/latest/. For more information, see Work with instance metadata.

First, connect to your running instance. From the instance, use this query to get its block device mapping.

IMDSv2
[ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/block-device-mapping/
IMDSv1
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/

The response includes the names of the block devices for the instance. For example, the output for an instance store–backed m1.small instance looks like this.

ami ephemeral0 root swap

The ami device is the root device as seen by the instance. The instance store volumes are named ephemeral[0-23]. The swap device is for the page file. If you've also mapped EBS volumes, they appear as ebs1, ebs2, and so on.

To get details about an individual block device in the block device mapping, append its name to the previous query, as shown here.

IMDSv2
[ec2-user ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/block-device-mapping/ephemeral0
IMDSv1
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/ephemeral0

First, connect to your running instance. From the instance, use this query to get its block device mapping.

PS C:\> Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/block-device-mapping/

The response includes the names of the block devices for the instance. For example, the output for an instance store–backed m1.small instance looks like this.

ami ephemeral0 root swap

The ami device is the root device as seen by the instance. The instance store volumes are named ephemeral[0-23]. The swap device is for the page file. If you've also mapped EBS volumes, they appear as ebs1, ebs2, and so on.

To get details about an individual block device in the block device mapping, append its name to the previous query, as shown here.

PS C:\> Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/block-device-mapping/ephemeral0