2

I am trying to connect to the amazon Rekognition service in region us-east-1. However boto3 always connects to us-west-2.

I have run $ aws configure list and the credentials and specified region seem to be correct there

enter image description here

I remember when I first filled in the credentials I did not specify a region and it defaulted to us-west-2. Then I edited the file in ~/.aws/config to change the region. It now has the following contents:

[default]
region = us-east-1

However when I run my script

import boto3
bucket='XX' 
photo='FOLDER/XX.jpg'
model='arn:aws:rekognition:us-east-1:XX:project/XX/version/XX/XX'
min_confidence=95
client=boto3.client('rekognition', region_name="us-east-1")
response = client.detect_custom_labels(Image={'S3Object':{'Bucket': bucket, 'Name': photo}},
        MinConfidence=min_confidence,
        ProjectVersionArn=model)

boto3 always connects to us-west-2

enter image description here

And then I get the following error:

An error occurred (InvalidParameterException) when calling the DetectCustomLabels operation: Cross region access not allowed

How can I tell boto3 to connect to a different region?

dude
  • 63
  • 7
  • 1
    thank you luk2302, I have updated my question with a more complete code snippet. – dude Mar 15 '22 at 10:38
  • 1
    Your bucket is probably in the wrong region / in us-west-2 / not in us-east-1 !? – luk2302 Mar 15 '22 at 10:41
  • I just checked that and both the model and the bucket with the image are in "us-east-1". The only difference between the buckets is that, the bucket with the images is not public. – dude Mar 15 '22 at 11:07
  • Why do you have the arn in bucket and object name? – Ninad Gaikwad Mar 15 '22 at 12:06
  • 1
    When does that screenshot appear? Is it displayed when you run the Python code? What do you mean by "connects to us-west-2"? – John Rotenstein Mar 15 '22 at 12:45
  • @John: The screenshot comes from my firewall, basically it just shows that boto3 tries to connect to "rekognition.us-west-2.amazonaws.com" and not to "rekognition.us-east-1.amazonaws.com". – dude Mar 15 '22 at 15:40
  • @Ninad: I tried the following combinations: arn:aws:s3:::BUCKETNAME, aws:s3:::BUCKETNAME, s3:::BUCKETNAME, BUCKETNAME. None of them worked. – dude Mar 15 '22 at 15:40
  • 1
    If you have any Rekognition resources in us-east-1 e.g. face collections then invoke the relevant describe or list method on that resource in us-east-1 to see if it yields the correct result (for resources in us-east-1). – jarmod Mar 15 '22 at 15:46
  • Thank you jarmod, I have now resolved the issue by placing the images I want to evaluate into a folder in the same bucket, where the model is stored. – dude Apr 07 '22 at 15:20

1 Answers1

0

This helped me. Add the line below to your script, before creating your client.

client = boto3.setup_default_session(region_name="us-east-1")
Alon Lavian
  • 1,149
  • 13
  • 14