Sáb. Jun 22nd, 2024

Although the AWS web interface can be enough for much people to manage their servers, if you have a infraestructure with development, test and production you likely to wish a way to manage it fast, flexible and schedulable.

I think that it’s very important you can use scripts to schedule boot an shutdown of servers, assign elastic ips, snapshots etc. and it’s a good way to control how long the servers are running and stop them when you aren’t  using them(for example development servers).

For the folowing explanation I’m goint to use Ubuntu distro,  Lucid version.

Setting up an Amazon account

You can associate your new EC2 account with an existing Amazon account (if you already have one), or create a new account.

  1. Go to http://aws.amazon.com, and select Sign-up Now. Sign in to your existing Amazon account or create a new one.
  2. Go to http://aws.amazon.com/ec2, and select «Sign Up for Amazon EC2».
    1. Enter your credit card information.
    2. Complete your signup for the Amazon EC2 service.
  3. After signing up, you should end up at the EC2 console
    1. Create a key pair and download the private key
      1. Click Key Pairs under Networking and Security in the Navigation pane and then click the Create Key Pair button (save it in e.g. ~/.ec2/ec2.pem). This private key is for making SSH connections to newly created instances.
    2. You will also need to set up your Amazon API credentials. Go to Account->Security Credentials
      1. click X.509 Certificates tab
      2. Create a new Certificate
      3. Download the private key and the certificate (save them in e.g. ~/.ec2/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem and ~/.ec2/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem).
      4. Make your credential files private: chmod go-rwx ~/.ec2/*.pem
      5. Scroll to the bottom of the page and note your account ID (a number of the form XXXX-XXXX-XXXX).

Installing the API tools

The EC2 API tools is now available for 9.04 users to install and configure the software. For previous versions of Ubuntu please see here.

  1. Make sure you have multiverse enabledand run the following command:
    sudo apt-get install ec2-api-tools

    If you’re not on the latest ubuntu release the packages may be a bit old. You can make use of the awstools ppa by doing:

    sudo apt-add-repository ppa:awstools-dev/awstools
    sudo apt-get update
    sudo apt-get install ec2-api-tools
  2. Make sure you have the following environment variables set up in your shell profile. This is accomplished by adding the following lines to your ~/.bashrc if you use bash as your shell:
    export EC2_KEYPAIR=<your keypair name> # name only, not the file name
    export EC2_URL=https://ec2.<your ec2 region>.amazonaws.com
    export EC2_PRIVATE_KEY=$HOME/<where your private key is>/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
    export EC2_CERT=$HOME/<where your certificate is>/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
    export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/


    1. See Common Options for API Tools for a description of what these environment variables do.
    2. The EC2_KEYPAIR is the Key Pair Name as listed in the AWS Management Console under Networking and Security -> Key Pairs, not the filename of the private key file that you saved to your local machine. This variable tells ec2 which SSH public key to insert into the instance during instantiation.
    3. The ec2 regions at the time of writing were:
Region URL
US-East (Northern Virginia) ec2.us-east-1.amazonaws.com
US-West (Northern California) ec2.us-west-1.amazonaws.com
EU (Ireland) ec2.eu-west-1.amazonaws.com
Asia Pacific (Singapore) ec2.ap-southeast-1.amazonaws.com
Asia Pacific (Tokyo) ec2.ap-northeast-1.amazonaws.com
  1. Load the changes into the current shell environment:
    source ~/.bashrc
  2. Check to see if it’s working by running the following command:
    ec2-describe-images -o self -o amazon

If all has been done right we will able to use all the commands referred to all of our instances, topic which I will probably explain in the next blogpost, so now we can start, stop, reboot and create instances and scripts  that take advantage of this operations.

For example we can boot and stop any instance you want using the following commands:

ec2-start-instances i-xxxxxxx

ec2-stop-instances i-xxxxxxx

0 comentario en “First steps with ec2-api-tools”

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *