I have a boto3 client :
boto3.client('kms')
But it happens on new machines, They open and close dynamically.
if endpoint is None:
if region_name is None:
# Raise a more specific error message that will give
# better guidance to the user what needs to happen.
raise NoRegionError()
Why is this happening? and why only part of the time?
This question is related to
python
linux
amazon-web-services
boto3
aws-kms
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.
os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'
In my case sensitivity mattered.
Alternatively you can run the following (aws cli)
aws configure --profile $PROFILE_NAME
it'll prompt you for the region.
notice in ~/.aws/config
it's:
[default]
region = ap-southeast-1
output = json
[profile prod]
region = ap-southeast-1
output = json
[profile profile name] in the square brackets
For those using CloudFormation template. You can set AWS_DEFAULT_REGION
environment variable using UserData and AWS::Region
. For example,
MyInstance1:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-04b9e92b5572fa0d1 #ubuntu
InstanceType: t2.micro
UserData:
Fn::Base64: !Sub |
#!/bin/bash -x
echo "export AWS_DEFAULT_REGION=${AWS::Region}" >> /etc/profile
I believe, by default, boto picks the region which is set in aws cli. You can run command #aws configure and press enter (it shows what creds you have set in aws cli with region)twice to confirm your region.
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.
Source: Stackoverflow.com