Select Page

Sometimes you end up with a situation where you want to start (or stop) an Amazon AWS instance from your own program instead of using the AWS interface.  It’s those occasions where something weird is going on in your systems when you want to handle a problem magically rather than having to go to the AWS management console.

There’s a Python module named boto3 that provides Python access to a variety of functions in AWS.  Some of the functions let you control EC2 instances, but there are other functions to control AWS features such as S3.  But let’s just focus on starting and stopping an AWS instance.  First, we need to install boto3 if we haven’t already:

Geeks can review the boto3 source code at Github.

Once you’ve got boto3 installed then you’ve taken a good step toward success.  The configuration documentation tells you that you can create a Shared Credentials File on your local drive.  The docs tell you “The shared credentials file has a default location of ~/.aws/credentials”.  But if you’re on Windows then you can’t create a directory or folder name that begins with a period.  What?!? What to do?  Darn it!

On Windows, you can put your credentials into a text file and specify the path to the file in the environment.  But to do that, you have to know how to set an environment variable.  Let’s look at how to do just that:

The contents of your credentials file is pretty straightforward:

Hop on into your EC2 console.  Click on your name in the top right to access the link to My Security Credentials.

Finding security credentials

Go ahead and click on Continue to Security Credentials because you’re feeling brave.  You’ll get through this.  Go ahead and click on Access Keys (Access Key ID and Secret Access Key) to expand that section.  Then continue your foolhardy bravery by clicking on Create New Access Key.  In the dialog that pops up you can Show Access Key and the kind folks from Amazon provide you with the Access Key ID and Secret Access Key that you can just paste right into your credentials file.  Personally I found this part the most difficult to figure out.  Once you’ve got the credentials setup you can move right into some code.

Connecting to your instances involves a few basic steps:

AWS needs to know your region.  We can set that in for the session.  I found the region name for my instances in the EC2 console.  Note that in the console my region is “us-west1c” but boto3 actually wanted just “us-west1” without the “c”.

Once you have your ec2 variable properly initialized then you can use it to communicate with EC2.  For example:

will return to you the list of your instances along with some status information.  Once you know the status of your instance then you can decide what to do with it.  You can iterate through the response object and its contents to view the properties you want. It’s not really rocket surgery.

Here’s the full example program:

Are you trying to control AWS instances programmatically?  How are you approaching these problems?

Share This