Setting region programmatically in Boto3

This entry is part 3 of 6 in the series Boto

At work I’m looking into the possibility of porting parts of our AWS automation codebase from Boto2 to Boto3. We desire to perform this port because Boto2’s record and result pagination appears defective.

I started to familiarize myself with Boto3 by using the Interactive Python interpreter. Here I show myself trying to connect to the RDS AWS endpoint following the docs:

>>> import boto3
>>> rds = boto3.client('rds')
Traceback (most recent call last):
...
NoRegionError: You must specify a region.

What? No region, ok how do I set a region? Well it turns out the docs want you to configure a region in a config file. This will not work for me, I need to set the region programatically…

So after stumbling around in the botocore source code I found the following 2 solutions.

Solution 1 – Set region_name when creating client:

>>> import boto3
>>> rds = boto3.client('rds', region_name='us-west-2')

Solution 2 – Set default region_name on the session:

>>> import boto3
>>> rds = boto3.setup_default_session(region_name='us-west-2')
>>> rds = boto3.client('rds')

It seems Boto3 has two types of interfaces, clients and resources.

Clients return description objects and appear lower level. Description objects seem like AWS XML responses transformed into Python Dicts/Lists.

Resources return higher level Python objects and like Instances with stop/start methods.

At a quick glance, both clients and resources seem to properly implement pagination automatically!

>>> # client interface.
>>> ec2 = boto3.client('ec2', region_name='us-west-2')
>>> idesc = ec2.describe_instances()
>>> len(idesc['Reservations'])
273
>>> idesc['ResponseMetadata']
{'HTTPStatusCode': 200, 'RequestId': '7e431ff7-xxxx-xxxx-xxxx-xxxxxxxxxxxxx'}

>>> # resource interface.
>>> ec2 = boto3.resource('ec2', region_name='us-west-2')
>>> for instance in ec2.instances.all():
>>>     print instance, instance.tags

That all for now, I hope this helps!

Series NavigationWorking with botocore’s ~/.aws/configBuild release pipelines on S3 with s3p

One thought on “Setting region programmatically in Boto3

  1. Nice work Russell!

    I was looking at setting the region within the filters for the describe_volume class.

    Turns out we have to specify it when establishing the client :)

Leave a Reply

Your email address will not be published. Required fields are marked *