One way or another you must tell boto3 in which region you wish the client to be created. This could be done explicitly using the kms parameter as in:region_name
kms = boto3.client('kms', region_name='us-west-2')
or you can have a default region associated with your profile in your file as in:~/.aws/config
[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.
s3 = boto3.client("s3", region_name="eu-west-1")
connects to S3 API endpoint in . It doesn't limit the listing to eu-west-1 buckets. One solution is to query the bucket location and filter.eu-west-1
s3 = boto3.client("s3")
for bucket in s3.list_buckets()["Buckets"]:
if s3.get_bucket_location(Bucket=bucket['Name'])['LocationConstraint'] == 'eu-west-1':
print(bucket["Name"])
If you need a one liner using Python's list comprehension:
region_buckets = [bucket["Name"] for bucket in s3.list_buckets()["Buckets"] if s3.get_bucket_location(Bucket=bucket['Name'])['LocationConstraint'] == 'eu-west-1']
print(region_buckets)
Thank you everyone for the input. @Michael-sqlbot's comment about the AWS client library defaulting to sending requests to the local region is what helped me find the solution. For Python, the library is boto3. Having read the docs it was not clear how to set the region. It was this blog post that provided the (simple) answer:
client = boto3.client('lambda', region_name='us-west-2')
You are right Michael that the use case for one lambda to another between regions is convoluted. I'll leave this answer here in case any others who are new to boto3 encounter the same error when trying to get other resources (lambda to ec2, lambda to s3, etc) to work across regions.
ThanksThere is many ways to do it. Refer to credential configuration guide for a start.
You can quickly get the info from from boto3.Session
# use aws credential profile
session = boto3.Session(profile_name='dev')
# Or hardcoded your credentail
session = boto3.Session(
aws_access_key_id="****",
aws_secret_access_key="****",
region_name="us-east-1"
)
Second way is supply hard coded credential in the client call. NOTE: You cannot specify profile_name using client.
client = boto3.client(
's3',
aws_access_key_id="****",
aws_secret_access_key="****",
region_name="us-east-1"
)
NOTE: If you setup EC2 instance using STS/temporary security credential, then you can retrieve the session token like this.
sts = boto3.client('sts')
my_token = sts.get_session_token()
s3 = boto3.client(
's3',
region_name="us-east-1",
aws_session_token = my_token
)