Coder Social home page Coder Social logo

ez2ec2's Introduction

Easy to EC2 (ez2ec2)

ez2ec2 is a collection of scripts for creating cheap ec2 spot instances easily and working with them.

Note

Alternatives

This script was written for the unique use case of easily creating a single Ubuntu spot instance on the cheapest availability zone on EC2 and also as a personal exercise in bashing. For other use cases, please use the highly extensible and actively developed AWS SDK Boto.

Credentials

This script uses aws configure to manage your aws credentials as detailed here and does not ask or use for them directly. Always read the code thoroughly before randomly executing scripts off the internet.

SSH private keys

This script can run from any folder, but creates a new SSH key pair in your home folder.

Prerequisites

  1. Create a new IAM user for ez2ec2 and get access and secret keys. You do not have to create a new user if you would like to use an existing IAM user. See here for instructions on how to do this. Download the credentials.csv file and keep it secure. You will need the access key and secret key later to configure aws.

  2. Allow the IAM user access to EC2 Role.

    • On your AWS web console, click on the newly created user name
    • Scroll down to Permissions
    • Click Attach Policy
    • Search, select, and attach the AmazonEC2FullAccess policy
  3. Install aws cli if you do not have it already. See more details here. pip install awscli

Instructions

Once you have an IAM user created and have their access key and secret access key, given the IAM user EC2 priviledges, and installed aws cli, to create a new EC2 instance:

  1. Set spot instance settings (at the top of the ez2ec2.sh script)

    # Spot instance settings
    SPOT_PRICE=0.071
    IMAGE_ID=ami-9a562df2
    INSTANCE_TYPE=m3.medium
    VOLUME_SIZE=16
    
    • Spot Price: Maximum spot price bid
      • Max bid of $0.071/hr
    • Image ID: AMI ID of instance to use
      • Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-9a562df2
    • Instance Type: HW type from here
      • Intel Xeon E5-2670, 1 vCPU, 3.75 GiB RAM, 4 GB SSD
    • Volume Size: Size in GiB of the EBS volume
      • 16 GiB
  2. Execute the script

    bash ez2ec2.sh

##What the script does

These are the different steps taken by the script

  1. Check if aws cli is installed
  2. Check if IAM credentials are already configured (or configure)
  3. Check if key pair already exists (or create)
  4. Check if key pair .pem file exists in home folder
  5. Check if security group exists (or create)
  6. Find cheapest spot instance availability zone
    1. Get pricing and subnet id for us-east-1a
    2. Get pricing and subnet id for us-east-1b
    3. Get pricing and subnet id for us-east-1d
    4. Get pricing and subnet id for us-east-1e
    5. Make sure cheapest option is less than limit set
  7. Allow SSH in security group from current IP address
  8. Request spot instance
  9. Poll to check if instance is ready to use
  10. Get and display instance details to connect

Expected Output

Sample initial output should look like

jim$ bash ez2ec2.sh
Checking if AWS CLI is installed
Using AWS CLI found at /Users/jim/anaconda/bin/aws

Checking aws iam credentials for ez2ec2usr
Using aws iam credentials for ez2ec2usr

Checking key pairs for ez2ec2key
Using existing key pair ez2ec2key

Checking home folder for ez2ec2key.pem
Using key pair file /Users/jim/ez2ec2key.pem

Checking security groups for ez2ec2grp
Using existing security group ez2ec2grp

Checking cheapest spot instance availability zone
Cheapest spot instance is in us-east-1d

Cheapest spot instance has price 0.008100
Cheapest spot instance is in subet subnet-b2854c99

Allowing SSH access from current IP address [hidden]

Creating spot request
Created spot request successfully

Spot request id is [hidden]

Polling to check if request has been fulfilled
This could take around 5 mins...

Use ctrl-c to stop polling and check aws console
Polling will automatically timeout in 10 mins
.
.
.
.
.
.
.

Instance id is [hidden]

Request has been fulfilled
Waiting to complete status checks
.
.
.
.
.
.
.

Instance has been initialized
Instance is ready to use

Connect to the instance using:
ssh -i ~/ez2ec2key.pem [email protected].[hidden]

jim$

ez2ec2's People

Contributors

jasimpson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.