Skip to content

Instantly share code, notes, and snippets.

@grenade
Created January 23, 2017 17:26
Show Gist options
  • Save grenade/51228c5e57a99a43efcf51d8dfaf53fd to your computer and use it in GitHub Desktop.
Save grenade/51228c5e57a99a43efcf51d8dfaf53fd to your computer and use it in GitHub Desktop.
ec2_profile="taskcluster"
ec2_region="us-east-1"
target_instance_id="i-00000000000000000"
shrink_instance_id="i-99999999999999999"
# determine the device name of the root volume (usually "/dev/sda1")
target_instance_device=$(aws ec2 describe-instance-attribute --profile ${ec2_profile} --region ${ec2_region} --instance-id ${target_instance_id} --attribute rootDeviceName | jq -r '.RootDeviceName.Value')
# determine the ec2 volume id of the root volume
large_volume_id=$(aws ec2 describe-instance-attribute --profile ${ec2_profile} --region ${ec2_region} --instance-id ${target_instance_id} --attribute blockDeviceMapping | jq -r '.BlockDeviceMappings[] | select(.DeviceName == "${target_instance_device}") | .Ebs.VolumeId')
# detach the large volume from the target instance
aws ec2 detach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${large_volume_id}
# determine the availability zone of the target host
target_instance_availability_zone=$(aws ec2 describe-instances --profile ${ec2_profile} --region ${ec2_region} --instance-ids ${target_instance_id} | jq -r '.Reservations[0].Instances[0].Placement.AvailabilityZone')
# create a new small volume in the availability zone of the target host
small_volume_id=$(aws ec2 create-volume --profile ${ec2_profile} --region ${ec2_region} --availability-zone ${target_instance_availability_zone} --size 30 --volume-type gp2 | jq -r '.VolumeId')
# attach the large volume to the shrink host as /dev/sdf
aws ec2 attach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${large_volume_id} --instance-id ${shrink_instance_id} --device /dev/sdf
# attach the small volume to the shrink host as /dev/sdg
aws ec2 attach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${small_volume_id} --instance-id ${shrink_instance_id} --device /dev/sdg
# determine the public dns name of the shrink host
shrink_instance_dns_name=$(aws ec2 describe-instances --profile ${ec2_profile} --region ${ec2_region} --instance-ids ${shrink_instance_id} | jq -r '.Reservations[0].Instances[0].PublicDnsName')
# determine the ssh key name of the shrink host
shrink_instance_key_name=$(aws ec2 describe-instances --profile ${ec2_profile} --region ${ec2_region} --instance-ids ${shrink_instance_id} | jq -r '.Reservations[0].Instances[0].KeyName')
# ssh to the shrink host and copy the large volume to the small one
ssh -i ~/.ssh/${shrink_instance_key_name} ec2-user@${shrink_instance_dns_name} sudo dd if=/dev/sdf of=/dev/sdg bs=1M
# detach the small volume from the shrink host
aws ec2 detach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${small_volume_id}
# attach the small volume to the target host
aws ec2 attach-volume --profile ${ec2_profile} --region ${ec2_region} --volume-id ${small_volume_id} --instance-id ${target_instance_id} --device ${target_instance_device}
@grenade
Copy link
Author

grenade commented Jan 23, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment