Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save suranands/212f57e9991f0d3e1b169eb6b8d77e20 to your computer and use it in GitHub Desktop.
Save suranands/212f57e9991f0d3e1b169eb6b8d77e20 to your computer and use it in GitHub Desktop.

Revisions

  1. Paul Nelson revised this gist Feb 1, 2011. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions ec2_instance_create_and_setup.sh
    Original file line number Diff line number Diff line change
    @@ -5,15 +5,30 @@
    #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`
    IP_ADDRESS=$(cat ~/.ec2/ip_address)

    # Create new t1.micro instance using ami-74f0061d (Basic 64-bit Amazon Linux AMI 2010.11.1 Beta)
    # 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 --key $EC2_INSTANCE_KEY --block-device-mapping "/dev/sda1=:16:true" --instance-initiated-shutdown-behavior stop --user-data-file instance_installs.sh ami-74f0061d`
    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/ .*//'`
    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

    @@ -26,4 +41,4 @@ echo
    ssh-keygen -R $IP_ADDRESS

    # SSH into my BRAND NEW EC2 INSTANCE! WooHoo!!!
    ssh -i $EC2_HOME/$EC2_INSTANCE_KEY.pem ec2-user@$IP_ADDRESS
    ssh -i $EC2_HOME/$EC2_INSTANCE_KEY.pem ubuntu@$IP_ADDRESS
  2. Paul Nelson created this gist Jan 30, 2011.
    29 changes: 29 additions & 0 deletions ec2_instance_create_and_setup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/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-74f0061d (Basic 64-bit Amazon Linux AMI 2010.11.1 Beta)
    # 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 --key $EC2_INSTANCE_KEY --block-device-mapping "/dev/sda1=:16:true" --instance-initiated-shutdown-behavior stop --user-data-file instance_installs.sh ami-74f0061d`

    INSTANCE_NAME=`echo ${EC2_RUN_RESULT} | sed 's/RESERVATION.*INSTANCE //' | sed 's/ .*//'`

    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 ec2-user@$IP_ADDRESS