Last updated: Feb 26, 2024
Reading time·3 min
The AWS and boto3 "botocore.exceptions.NoRegionError: You must specify a region" error occurs because you must specify the region in which the boto3 client should be created.
You can set the region in multiple ways:
region_name
argument when creating the boto3 client.~/.aws/config
file.AWS_DEFAULT_REGION
environment variable via your shell.AWS_DEFAULT_REGION
environment variable in your Python
script.region_name
argument when creating the boto3 clientOne way to solve the error is to pass the region_name
argument when creating
the boto3
client.
Here is an example that does that when creating an s3
client.
import boto3 region = 'us-east-1' s3_client = boto3.client('s3', region_name=region) # 👇️ print region name print(s3_client.meta.region_name) print(s3_client)
The boto3.client()
method creates a low-level service client.
The method takes a region_name
argument that determines in which region the
client should be created.
I used us-east-1
in the example, but you can use any other region.
~/.aws/config
fileAlternatively, you can set the default region directly in your ~/.aws/config
configuration file.
[default] region = us-east-1
The ~/.aws/config
path is a shorthand for /home/YOUR_USER/.aws/config
.
You can use gedit
, nano
or vim
to edit your config file and add the
default region.
# gedit sudo gedit ~/.aws/config # nano sudo nano ~/.aws/config # vim sudo vim ~/.aws/config
Once you open the file in your preferred text editor, set your default region.
[default] region = us-east-1
Once you've set your default region in ~/.aws/config
, you are no longer
required to provide the region_name
argument when creating a client.
import boto3 region = 'us-east-1' s3_client = boto3.client('s3') print(s3_client.meta.region_name) print(s3_client)
As shown in the code sample, the region is taken from my ~/.aws/config
file.
You can also run the aws configure
command in your shell to set your default
AWS region.
aws configure
Press Enter
until you get prompted for the region and specify your preferred
default region.
AWS_DEFAULT_REGION
environment variable in your shellYou can also specify the default region by setting the AWS_DEFAULT_REGION
environment variable in your shell.
If you are on macOS or Linux, issue the following command.
export AWS_DEFAULT_REGION=us-east-1
us-east-1
.The next time you create a boto3 client, the default region will be taken from
the value of your AWS_DEFAULT_REGION
environment variable.
import boto3 s3_client = boto3.client('s3') print(s3_client.meta.region_name) print(s3_client)
If you need to set the environment variable in a Docker container, you would use
the ENV
prefix.
ENV AWS_DEFAULT_REGION=us-east-1
AWS_DEFAULT_REGION
environment variable in your Python scriptYou can also set the AWS_DEFAULT_REGION
environment variable directly in your
Python script.
import os import boto3 os.environ['AWS_DEFAULT_REGION'] = 'us-east-1' s3_client = boto3.client('s3') print(s3_client.meta.region_name) # 👉️ us-east-1 print(s3_client)
The code sample uses the
os.environ mapping
object to set the AWS_DEFAULT_REGION
environment variable to us-east-1
.
Note that the line that sets the environment variable has to run before you
create your boto3
client.
If you need to create the boto3
client in the same region as your Lambda
function, access the AWS_REGION
environment variable to get the current
region.
import os import boto3 def main(event, context): lambda_region = os.environ['AWS_REGION'] print(f'Lambda function region {lambda_region}') s3_client = boto3.client('s3', region_name=lambda_region)
Once you get the current Lambda region, supply the region_name
argument in the
call to boto3.client()
.
I've also written an article on how to solve the error when using the AWS CLI.
You can learn more about the related topics by checking out the following tutorials: