Ubuntu: Attach EBS Volume & Mount it to a directory

Assume you have installed AWS Command Line Interface and you instance is started with appropriate AWS IAM Role (so you don’t need explicitly provide credentials).

# 1. Make a directory to serve as a mount point
mkdir -p /data
chown `id -u` /data
# 2. Attach EBS volume to the instance
export AWS_DEFAULT_REGION=us-east-1
VolumeId=$(aws ec2 describe-volumes --filters Name='tag:Name',Values='My Back-End' | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["Volumes"][0]["VolumeId"]')
InstanceId=$(wget -q -O -
aws ec2 attach-volume --volume-id $VolumeId --instance-id $InstanceId --device /dev/xvdf
ls /dev/xvdf
# 3. Wait till volume attached to the instance
while [ $? -ne 0 ]; do
sleep 5
ls /dev/xvdf
# 4. Mount the volume
mount -a /dev/xvdf /data

BTW: Why do I need this? I want to use AWS EC2 Spot Instance for my PostgreSQL. So I had to come up with solution to persist data when my instance is terminated. I decided to go with AWS EBS volume.

UPDATE: PosrgreSQL available as AWS RDS. So you don’t need to use solution above in case of PostgreSQL.

One thought on “Ubuntu: Attach EBS Volume & Mount it to a directory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: