Skip to content

Instantly share code, notes, and snippets.

@nmagee
Created April 12, 2015 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmagee/72f5d9ba12efeca40b63 to your computer and use it in GitHub Desktop.
Save nmagee/72f5d9ba12efeca40b63 to your computer and use it in GitHub Desktop.
Create an EC2 AMI using your InstanceId
#!/bin/sh
# This script creates a series of prompts to create an AMI of an EC2 instance from a few AWS profiles
# Note that the YYYYmmdd- is added to the name of the AMI automatically
set -e
clear
# Check for AWS CLI tools
type aws >/dev/null 2>&1 || { echo >&2 "\n\n I require the AWS CLI but it's not installed. Please visit http://aws.amazon.com/cli/ and install it. Aborting.\n"; exit 1; }
# Interview the user
echo ""
read -p " What AWS profile does this instance belong to? (Leave blank for default): " profile
read -p " What is the Instance ID of the server you want to AMI: " instanceid
read -p " What is the name you want to give this AMI? (No spaces - DATE is prepended): " aminame
read -p " Describe this AMI in plain language (Max 256 chars): " amidesc
read -p " Is it okay to reboot the instance? (y/N): " rebootx
case $rebootx in
[Yy]* ) rebootv="--reboot"; break;;
[Nn]* ) rebootv="--no-reboot";;
* ) echo "Please answer Y or N";;
esac
# Assemble a date prefix + the ami name
amidatename=`date +%Y%m%d`-$aminame
# Give feedback to the user and teach them what the manual call would look like
echo "\n AWS Profile: $profile"
echo " Instance ID: $instanceid"
echo " AMI Name: $amidatename"
echo " AMI Description: $amidesc"
echo " Reboot: $rebootx\n"
echo " The API call being made to AWS is:"
echo " aws --profile $profile ec2 create-image --instance-id $instanceid --name $amidatename --description \"$amidesc\" $rebootv"
# Make the actual call
aws --profile $profile ec2 create-image --instance-id $instanceid --name $amidatename --description "$amidesc" $rebootv
# Confirm. TODO - create a ping query to let the user know when AMI is complete.
echo "Your AMI request has been queued. Please wait for it to complete."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment