Quickly fix botocore.exceptions.noregionerror: you must specify a region.

Quickly diagnose and fix the botocore.exceptions.noregionerror: you must specify a region error. The fix is you must specify a region.

The quickest fix is to open your ~/.aws/config and add region=us-east-1 to the [default] profile. There are more details and several other ways to specify a region at the bottom of this article in the How To Fix section.

botocore.exceptions.noregionerror: you must specify a region.
Here is how your config file should look. This will resolve botocore.exceptions.noregionerror: you must specify a region

Diagnosing the NoRegionError

I received this error while running PyTest running in GitHub actions.

The full text of the error was:

self = <botocore.regions.EndpointResolver object at 0x7f727692bca0>
partition = OrderedDict([('defaults', OrderedDict([('hostname', '{service}.{region}.{dnsSuffix}'), ('protocols', ['https']), ('sig...1', OrderedDict()), ('us-east-2', OrderedDict()), ('us-west-1', OrderedDict()), ('us-west-2', OrderedDict())]))]))]))])
service_name = 'dynamodb', region_name = None, force_partition = False

    def _endpoint_for_partition(self, partition, service_name, region_name,
        # Get the service from the partition, or an empty template.
        service_data = partition['services'].get(
            service_name, DEFAULT_SERVICE_DATA)
        # Use the partition endpoint if no region is supplied.
        if region_name is None:
            if 'partitionEndpoint' in service_data:
                region_name = service_data['partitionEndpoint']
>               raise NoRegionError()
E               botocore.exceptions.NoRegionError: You must specify a region.

/usr/local/lib/python3.8/dist-packages/botocore/regions.py:148: NoRegionError

The specific code causing the error was:

dynamodb = boto3.resource('dynamodb')

This issue did not occur on my local machine, where I have an AWS Config file specifying the region, or deployed in Lambda. Instead, the error only occurred in my GitHub action CI while running my pytest unit tests.

My Resolution for botocore.exceptions.noregionerror: you must specify a region

The simplest fix is just to pass in a default region when running pytest as shown below:

AWS_DEFAULT_REGION=us-east-1 python -m pytest tests/unit -v

You could also set as an environment variable in GitHub actions but that may have unintended consequences.

Here is a GitHub issue addressing the issue.

How To Fix botocore.exceptions.noregionerror

As shown above, the trick is to make sure you provide a region. There are a few ways to do it.

Fix 1: Specify Region in AWS config file for Boto3

Just specify the region in your aws configs file, boto3 will pick the region up from your config file automatically.

On Mac/Linux this is located at ~/.aws/config
Simply edit this file and add region=us-east-1. See the example below with 2 profiles configured, both with us-east-1 specified as the region.



botocore.exceptions.noregionerror: you must specify a region.

Fix 2: Specify Region in Environment Variable for Boto3

Put the region in an environment variable, as shown in the command running the python unit tests. There are 2 ways to do this.

  • Set the systems environment variables with AWS_DEFAULT_REGION=us-east-1
  • Pass an env var into the command when running python, as shown in the previous section

Fix 3: Specify the Region in the Code

Hardcore a region in the code, this is not recommended but will fix the issue

import boto3
from botocore.config import Config

my_config = Config(
    region_name = 'us-west-2'

client = boto3.client('kinesis', config=my_config)

Here are the boto3 AWS docs for more details on hard-coding the region.

Leave some feedback, please

If this helped you please leave a comment on how you fixed this issue. Still need help, leave a comment too.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.