Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A bash script that will set up a new EC2 instance and ssh into it.
#!/bin/bash
# Authorize TCP, SSH & ICMP for default Security Group
#ec2-authorize default -P icmp -t -1:-1 -s 0.0.0.0/0
#ec2-authorize default -P tcp -p 22 -s 0.0.0.0/0
# The Static IP Address for this instance:
IP_ADDRESS=$(cat ~/.ec2/ip_address)
# Create new t1.micro instance using ami-cef405a7 (64 bit Ubuntu Server 10.10 Maverick Meerkat)
# using the default security group and a 16GB EBS datastore as /dev/sda1.
# EC2_INSTANCE_KEY is an environment variable containing the name of the instance key.
# --block-device-mapping ...:false to leave the disk image around after terminating instance
EC2_RUN_RESULT=$(ec2-run-instances --instance-type t1.micro --group default --region us-east-1 --key $EC2_INSTANCE_KEY --block-device-mapping "/dev/sda1=:16:true" --instance-initiated-shutdown-behavior stop --user-data-file instance_installs.sh ami-cef405a7)
INSTANCE_NAME=$(echo ${EC2_RUN_RESULT} | sed 's/RESERVATION.*INSTANCE //' | sed 's/ .*//')
times=0
echo
while [ 5 -gt $times ] && ! ec2-describe-instances $INSTANCE_NAME | grep -q "running"
do
times=$(( $times + 1 ))
echo Attempt $times at verifying $INSTANCE_NAME is running...
done
echo
if [ 5 -eq $times ]; then
echo Instance $INSTANCE_NAME is not running. Exiting...
exit
fi
ec2-associate-address $IP_ADDRESS -i $INSTANCE_NAME
echo
echo Instance $INSTANCE_NAME has been created and assigned static IP Address $IP_ADDRESS
echo
# Since the server signature changes each time, remove the server's entry from ~/.ssh/known_hosts
# Maybe you don't need to do this if you're using a Reserved Instance?
ssh-keygen -R $IP_ADDRESS
# SSH into my BRAND NEW EC2 INSTANCE! WooHoo!!!
ssh -i $EC2_HOME/$EC2_INSTANCE_KEY.pem ubuntu@$IP_ADDRESS
@Pablosan

This comment has been minimized.

Copy link
Owner Author

Pablosan commented Jan 30, 2011

This script is the result of my desire to be able to create a brand new EC2 instance, perform a bunch of installations and setup, and SSH into the instance: all by typing a single command at a command prompt. This approach provides a couple advantages over creating an Instance Snapshot on AWS:

• I don't have to remember all the nit-picky details of getting a new instance up and configured
• Every minute detail will happen the same way every time
• The script serves as a reminder of every step taken to start and configure the instance
• You don't have to pay the AWS charges for Snapshot storage!

You will need to create an accompanying script – instance_installs.sh – that contains all of the directory creation, yum installs, configuration, etc.

@Pablosan

This comment has been minimized.

Copy link
Owner Author

Pablosan commented Feb 1, 2011

Version 2!
• Now uses the official 64 bit Ubuntu Server 10.10 (Maverick Meerkat) AMI (in us-east-1)
• The default AWS Linux AMI didn't seem to support the --user-data-file option
• Now checks to make sure instance is running before trying to assign it a static IP address
• Changed all `'s (tick) usage to the preferred $() usage
• If the check for running status fails five times, the script exits early with a friendly message

@vinzenzweber

This comment has been minimized.

Copy link

vinzenzweber commented Dec 7, 2013

Nice, I am just working on setup scripts for my server and appreciate your work!

@sethbergman

This comment has been minimized.

Copy link

sethbergman commented Nov 16, 2015

This is excellent! Just what I was looking for!

@spicysomtam

This comment has been minimized.

Copy link

spicysomtam commented Oct 6, 2016

Latest aws cli the commands are slightly different. Instead of:
ec2-describe-instances

You would have:
aws ec2 describe-instances

Also instead of ~/.ec2 you have ~/.aws.

Also the security is somewhat different. You need to go into IAM, add a user, give it permissions like AdministratorAccess and then generate an Access Key (and secret), and then install these keys on your client via 'aws configure'

@harishbandari

This comment has been minimized.

Copy link

harishbandari commented Nov 17, 2016

i am unable to create a new instance with this script and please help me

Email:harish.bandari143@gmail.com

Thanks and regards
Harish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.