Last active
June 5, 2016 21:29
-
-
Save kjenney/c4537596c0eb3ca0d88419405d3cf40a to your computer and use it in GitHub Desktop.
EC2 Create Instance and Resize Filesystem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Tester script to create an EC2 instance and resize the filesystem | |
function resizeit { | |
echo "--------------------- RESIZING ---------------------" | |
echo | |
echo -e "\tPowering down the instance - $awsinstanceid" | |
ec2-stop-instances $awsinstanceid > /dev/null 2>&1 | |
aws ec2 wait instance-stopped --instance-id $awsinstanceid | |
echo -e "\t\tInstance is stopped" | |
awsvolumeid=$(ec2-describe-volumes -F "attachment.instance-id=$awsinstanceid" | grep ATTACHMENT | awk '{print $2'}) | |
if [ -z $awsvolumeid ]; then | |
"No volume is attached to the instance" | |
exit 1 | |
fi | |
echo -e "\tDetaching the volume - $awsvolumeid" | |
ec2-detach-volume $awsvolumeid > /dev/null 2>&1 | |
echo -e "\t\tVolume is detached" | |
echo -e "\tTake a snapshot of the instance disk" | |
ec2-create-snapshot $awsvolumeid > /dev/null 2>&1 | |
awssnapshotid=$(ec2-describe-snapshots -F "volume-id=$awsvolumeid" | awk '{print $2'}) | |
aws ec2 wait snapshot-completed --snapshot-ids $awssnapshotid | |
echo -e "\t\tSnapshot completed - $awssnapshotid" | |
echo -e "\tCreate a new EBS volume from the snapshot with the specified size" | |
newvolid=$(ec2-create-volume --snapshot $awssnapshotid --size $disk -z us-west-1a | grep VOLUME | awk '{print $2}') | |
aws ec2 wait volume-available --volume-ids $newvolid | |
echo -e "\tAttach the new volume to the instance - $newvolid" | |
ec2-attach-volume $newvolid -i $awsinstanceid -d /dev/sda1 > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
echo -e "\t\tVolume attached succesfully" | |
else | |
echo -e "\t\tSomething went wrong" | |
exit 1 | |
fi | |
echo -e "\tPower on the instance" | |
ec2-start-instances $awsinstanceid > /dev/null 2>&1 | |
aws ec2 wait instance-running --instance-ids $awsinstanceid | |
echo -e "\t\tInstance is started" | |
sleep 20 # Wait for SSH | |
echo -e "\tResize partition" | |
ssh root@$aws_ip "fdisk /dev/xvda << EEOF | |
d | |
n | |
p | |
1 | |
2 | |
w | |
EEOF" >> /dev/null 2>&1 | |
ssh root@$aws_ip "shutdown -h 0" | |
aws ec2 wait instance-stopped --instance-ids $awsinstanceid | |
ec2-start-instances $awsinstanceid > /dev/null 2>&1 | |
aws ec2 wait instance-running --instance-ids $awsinstanceid | |
sleep 20 # Wait for SSH | |
echo -e "\tResize filesystem" | |
ssh root@$aws_ip "resize2fs -p /dev/xvda1" | |
if [ $? -eq 0 ]; then | |
echo -e "\tResize succesfully completed" | |
else | |
echo -e "\tResize failed" | |
exit 1 | |
fi | |
# Cleanup | |
ec2-delete-snapshot $awssnapshotid | |
ec2-delete-volume $awsvolumeid | |
} | |
function createvm { | |
awsinstanceid=$(ec2-run-instances $ami -k $awsprivatekey -g $securitygroup --instance-type $instancetype -s $awssubnet --private-ip-address $aws_ip | grep INSTANCE | awk '{print $2}') | |
if [ $? -eq 0 ]; then | |
echo -e "\tSuccessfully created...waiting for the vm to come up" | |
echo -e "\t\tHere's your instance ID: $awsinstanceid" | |
ec2addtag $awsinstanceid --tag Name=$myhost > /dev/null 2>&1 | |
aws ec2 wait instance-running --instance-ids $awsinstanceid | |
echo -e "\tServer is up and ready...Let's resize" | |
echo | |
else | |
echo -e "\tFailed to create VM. Please check your configuration. Error code was $?" | |
fi | |
} | |
awsprivatekey="mykey" | |
ami="ami-12345678" | |
securitygroup="sg-12345678" | |
instancetype="t2.micro" | |
awssubnet="subnet-12345678" | |
aws_ip="10.10.0.2" | |
myhost="test" | |
disk=20 | |
createvm | |
resizeit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment