Boto3: NoRegionError


Read on if your boto3 code is throwing this error:

botocore.exceptions.NoRegionError: You must specify a region.

But first, two reasons why you might only get this error in some situations.

AWS lambda sets the region automatically. Code that works in lambda may still throw NoRegionError locally.

Some AWS services are global (don’t have regions). Like S3:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-buycket')
for object in bucket.objects.all():

This code will work without setting a region.

Those are edge cases. You’ll usually get a NoRegionError unless you set a region. Like from this script on a laptop (it would work in lambda):

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

The docs have all the details of setting a region, but the cheap and easy answer is to add this to the top of your ~/.aws/config file (create it if it doesn’t exist):

region = us-west-2

This sets us-west-2 as an example. Replace that with the region you’re using.

There are more robust options and as you get farther with boto3 I recommend checking out the details in the link above, but this should get you moving again.

Happy automating!


Need more than just this article? We’re available to consult.

You might also want to check out these related articles: