AWS with python

In my previous post, I set up my Mac to do python development.  Now I’m ready to launch an EC2 image using boto.

Create Virtual Python Environment

I have already logged into AWS and created an account.  I gave it a credit card and now I get a free server for a year that I can do all sorts of fun things.

First, I’m going to create a virtual environment for this:

Connect to AWS

So now we have the base setup.  I checked the documentation for AWS here:

It said I needed some security credentials, but I noticed that I don’t have any.  I logged onto AWS and I created a user for my account named tester.  From there, I saw the access key ID and secret access key.  After that, I had to give him administration permissions.

I ran the command as shown in the document:

We are in!

Launch an instance

From the AWS management console when we look at the image store, I found the Ubuntu 14.04 LTS was available in the free tier. Next to the name, I found the instance id: ami-3d50120d. So that’s the one I’m going to try to launch. We also need to set the size. From the console, I found that the t2.micro instance was also free. So I gave that a whirl.  Let’s get the image going and then check how its doing.

I found that I could then see what the status was again by looking a little later:

Notice, I had to refresh the statuses variable.  The next step is to figure out how we can log into the server now that its up.   We could go to the EC2 management portal and create a keypair.  Or perhaps we could try to do this pragmatically?

Create a Boto Configuration File

To start off with, we’d like to put our environment variables in a  config file so we don’t have to enter that into the script.  To do this we create a file:

This file will be referenced by the APIs so we don’t have to provide the information next time.  The format of the file is:

That looks ok. Let’s log in again and terminate the instance:

Create Key Pair for future instances


Create the New Instance with our Keypair

That last command gives us the IP address of the server. So let’s log into him now.

Login to new instance

If we try to ping, we realize that we can’t.  The problem is, we didn’t give it a security group.  The default security group has locked it down from the outside world.  What we need to do is enable some ports on it.  Primarily, we need to enable SSH, port 22.  We’ll do this from the console at this point and leave the API calls to you gentle reader to figure out.  Once that is done, we could do a simple

ssh  -i ~/Desktop/tester-keypair

But that’s kind of a pain.  What we could do instead is create a config file so we only have to run a simpler command.  To do this, we copy the keypair file into the ~/.ssh directory.

Then we create a file called ~/.ssh/config.  We then make it look like the following:

Now we can log right in!


A few things I need to go back and check on:

  1. Creating the default Security group.  This shouldn’t be too hard.
  2. Making the pem file save.  In this article I think there is a bug using python3 and the command as I wasn’t able to write out the file.  I kept getting the exception: TypeError: ‘str’ does not support the buffer interface.

Comments are closed.