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).
—-
#!/bin/bash # 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 - http://169.254.169.254/latest/meta-data/instance-id) 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 done # 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.
Reblogged this on Sutoprise Avenue, A SutoCom Source.