boto3 client NoRegionError: You must specify a region error only sometimes
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3
--
Chapters
00:00 Boto3 Client Noregionerror: You Must Specify A Region Error Only Sometimes
00:22 Accepted Answer Score 698
00:56 Answer 2 Score 15
01:16 Answer 3 Score 34
01:25 Answer 4 Score 11
01:59 Thank you
--
Full question
https://stackoverflow.com/questions/4037...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #linux #amazonwebservices #boto3 #awskms
#avk47
ACCEPTED ANSWER
Score 698
One way or another you must tell boto3 in which region you wish the kms client to be created.  This could be done explicitly using the region_name parameter as in:
kms = boto3.client('kms', region_name='us-west-2')
or you can have a default region associated with your profile in your ~/.aws/config file as in:
[default]
region=us-west-2
or you can use an environment variable as in:
export AWS_DEFAULT_REGION=us-west-2
but you do need to tell boto3 which region to use.
ANSWER 2
Score 34
os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'
In my case sensitivity mattered.
ANSWER 3
Score 15
you can also set environment variables in the script itself, rather than passing region_name parameter
os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'
case sensitivity may matter.
ANSWER 4
Score 11
For Python 2 I have found that the boto3 library does not source the region from the ~/.aws/config if the region is defined in a different profile to default.
So you have to define it in the session creation.
session = boto3.Session(
    profile_name='NotDefault',
    region_name='ap-southeast-2'
)
print(session.available_profiles)
client = session.client(
    'ec2'
)
Where my ~/.aws/config file looks like this:
[default]
region=ap-southeast-2
[NotDefault]
region=ap-southeast-2
I do this because I use different profiles for different logins to AWS, Personal and Work.