AWS: Installing the AWS SDK for Python on Ubuntu

Amazon EC2 provides a web interface for managing IaaS, but for repeatable infrastructure deployment what you really want is the ability to deploy and manage this infrastructure using an API or command line tool.

The AWS SDK for Python (Boto3) provides a lower level as well as resource level API for managing and creating infrastructure.  This provides an automated and repeatable way to create environments for production or testing.

Prerequisites

As a prerequisite, install the AWS CLI using the instructions from my previous article.  This ensures you have Python installed correctly, sets up the configuration/credentials file, and provides a smoke test that shows you have your IAM key properly configured.

With this complete, you should be able to run a quick smoke test from inside the virtualenv to show the available regions.

(awscli) $ aws ec2 describe-regions

AWS SDK for Python

We will use the same Python virtualenv described in my previous article for the AWS CLI.

# start virtual environment previously created for awscli
source awscli/bin/activate

# install Boto3 inside virtualenv
(awscli) $ pip install boto3 --upgrade

Now use my smoke test, list_aws_regions.py from github to show the available regions.

# get script from github
(awcli) $ wget https://raw.githubusercontent.com/fabianlee/blogcode/master/vagrant/awscli1604/list_aws_regions.py

(awscli) $ python list_aws_regions.py

Name: ap-south-1       Endpoint: ec2.ap-south-1.amazonaws.com
Name: eu-west-3        Endpoint: ec2.eu-west-3.amazonaws.com
Name: eu-north-1       Endpoint: ec2.eu-north-1.amazonaws.com
Name: eu-west-2        Endpoint: ec2.eu-west-2.amazonaws.com
Name: eu-west-1        Endpoint: ec2.eu-west-1.amazonaws.com
Name: ap-northeast-2   Endpoint: ec2.ap-northeast-2.amazonaws.com
Name: ap-northeast-1   Endpoint: ec2.ap-northeast-1.amazonaws.com
Name: sa-east-1        Endpoint: ec2.sa-east-1.amazonaws.com
Name: ca-central-1     Endpoint: ec2.ca-central-1.amazonaws.com
Name: ap-southeast-1   Endpoint: ec2.ap-southeast-1.amazonaws.com
Name: ap-southeast-2   Endpoint: ec2.ap-southeast-2.amazonaws.com
Name: eu-central-1     Endpoint: ec2.eu-central-1.amazonaws.com
Name: us-east-1        Endpoint: ec2.us-east-1.amazonaws.com
Name: us-east-2        Endpoint: ec2.us-east-2.amazonaws.com
Name: us-west-1        Endpoint: ec2.us-west-1.amazonaws.com
Name: us-west-2        Endpoint: ec2.us-west-2.amazonaws.com

If you are on Python2.x you will see an SNIMissingWarning coming from urllib3, this can be ignored.  Upgrade to Python3 if you want to avoid this message.

Python code

The code for list_aws_regions.py is succinct, creating a client then invoking describe_regions() to get the region list.

import boto3
ec2 = boto3.client('ec2')

response = ec2.describe_regions()

# prints each region details
for region in response['Regions']:
  print("Name: {:16} Endpoint: {}".format(region['RegionName'],region['Endpoint']))

IAM Permissions

When we created the IAM user policy in my previous article, we only set “AmazonEC2FullAccess”.  If you are going to manage other resources such as S3 using the Python SDK, make sure you add these permissions to the user policy.

 

REFERENCES

Amazon SDK for Python

Boto3 Documentation