Skip to content

Instantly share code, notes, and snippets.

@matt2000
Created February 22, 2017 00:38
Show Gist options
  • Save matt2000/f78cd068b1d58f019d9c410663032c1b to your computer and use it in GitHub Desktop.
Save matt2000/f78cd068b1d58f019d9c410663032c1b to your computer and use it in GitHub Desktop.
#!/bin/sh
# Usage: run-with-temp-vol.sh SCRIPT SIZE MOUNT [DEVICE]
### Run this from an EC2 node to run the command given as $1 after we provision
### and attach an EBS volume of size $2 at mount point $3 as device $4, default
### "/dev/xvdm".
# Params
SCRIPT="$1"
SIZE="$2"
DEVICE="/dev/xvdm"
MOUNT="$3"
if [ -z $4 ]; then
DEVICE="$4"
fi
echo "Creating Volume..."
VOLUME_ID=`aws ec2 create-volume \
--availability-zone us-west-2a \
--size $SIZE \
| jq -r '.VolumeId'` \
&& echo "Created $VOLUME_ID; Attaching to slave node..."
INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id`
aws ec2 attach-volume \
--volume-id $VOLUME_ID \
--instance-id $INSTANCE_ID \
--device $DEVICE \
&& echo "Attached as $DEVICE. Mounting to $MOUNT..."
mkdir $MOUNT
sudo mount $DEVICE $MOUNT \
&& echo "OK."
echo "Running script..."
`$SCRIPT` && echo "OK."
echo "Deleting volume $VOLUME_ID..."
aws ec2 delete-volume --volume-id $VOLUME_ID
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment