Block Device Mapping and EBS Voume

Block Device Mapping Information

For an instance

Block device named /dev/sda1 with EBS vol-b0633bd9 are created at 2010-09-07 and attached to instance-id

ec2-describe-instances instance-id

...
BLOCKDEVICE     /dev/sda1       vol-b0633bd9    2010-09-07T17:41:58.000Z

For an AMI

Block device /dev/sda1 of size 15GB will initiate with data from snapshot snap-c07d1cab

ec2-describe-images ami_id

...
BLOCKDEVICEMAPPING      /dev/sda1               snap-c07d1cab   15

DeleteOnTermination

By default, the root device volume for an EBS backed AMI is deleted when an instance terminates

  • To terminate an instance without deleting the volume
    ec2-modify-instance-attribute i-5d555555 -b /dev/sda1=vol-555555e5:false -v
    

    The same command can be used to change the DeleteOnTermination value of a volume

Create, Attach & Mount an Amazon EBS volume to an EC2 Instance

  1. Create a EBS Volume in AWS Management Console
    1. Select Amazon EC2 -> Volumes
    2. Click Create Volume
    3. The volume must be 2GB or larger
    4. Select the same availability zone as your instance
  2. Attach the volume to an instance in AWS Management Console
    1. Select the new volume and click Attach Volume
    2. Name the device say /dev/sdh
  3. Create and mount a file system
    yes | mkfs -t ext3 /dev/sdh
    mkdir /mnt/your-location
    mount /dev/sdh /mnt/your-location
    

Create an ESB based EC2 AMI from an EC2 Instance

  1. Login to AWS Management Console
  2. In the left panel, select Instances
  3. Right click on the instance to be used
    1. Stop the instance
    2. Create Image (EBS AMI)
  4. Monitor the AMI status until it changes to available
  5. Start the instance again

Create a S3 based EC2 AMI from an EC2 Instance

  1. Log into the EC2 instance
  2. Bundle an EC2 instance
    ec2-bundle-vol -k /home/ubuntu/key/pk-aws.pem -c /home/ubuntu/key/cert-aws.pem -u 1111-2222-3333
    

    Replace with your proper key/certicate and AWS account number

  3. Upload the image to S3
    ec2-upload-bundle -b your_own_company_name.my-image -m /tmp/my-image.fs.manifest.xml \
    -a access-key-id -s secret-access-key
    

    Replace with the proper id/secret-key & company name

  4. Register the AMI
    ec2-register your_own_company_name.my-image/my-image.fs.manifest.xml -n centos_image
    

Override the Block Device Mapping when Create & Register an AMI

Define a new Block Device Mapping for an AMI

ec2-register -n My_Image_Name -d My_image_description \
--root-device-name /dev/sda1  \
-b /dev/sda1=snap-12345678:80:false \
-b /dev/sdc=ephemeral1

The new AMI will

  • Use /dev/sda1 as the boot device
  • Use snapshoot snap-12345678 to initialize the device /dev/sda1
  • /dev/sda1 will not be deleted on termination
  • Create a device /dev/sdc using a local instance storage
ec2-register -n My_Image_Name -d My_image_description \
--root-device-name /dev/sda1 \
-b /dev/sda1=snap-55555555:40:false \
-b /dev/sdh=snap-55555555 \
-b /dev/sdk=:20:false
  • Create 3 Block Device
  • Increase /dev/sda1 to 40G
  • Increase /dev/sdk to 20G

Override AMI's Block Device Mapping when Launch an EC2 Instance

Map a block device name /dev/sdb to the local instance storage ephemeral0

ec2-run-instances ami-1234de7b -b /dev/sdb=ephemeral0 -k webserver -g webserver

RESERVATION     r-14e3d57f      558556443206    webserver
INSTANCE        i-9586d7ff      ami-1234de7b                    pending webserver       0
m1.small        2010-09-07T19:12:45+0000        us-east-1c      aki-5037dd39
monitoring-disabled                                    ebs

This is for illustration only. ami-1234de7b already map an instant local storage to /dev/sda2

df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              15G  704M   14G   5% /
devtmpfs              834M  116K  834M   1% /dev
none                  851M     0  851M   0% /dev/shm
none                  851M   48K  851M   1% /var/run
none                  851M     0  851M   0% /var/lock
none                  851M     0  851M   0% /lib/init/rw
/dev/sdb              147G  188M  140G   1% /mnt
ec2-run-instances ami-5555555 \
-b /dev/sdh=:80:true \
-b /dev/sdj=none \
-b /dev/sdb=ephemeral0
  • Increase /dev/sdh to 80G and set DeleteOnTermination to true
  • Do not map /dev/sdj
  • Map device /dev/sdb to the local instance storage ephemeral10