IllegalLocationConstraintException error when using s3 create_bucket code

0

In aws cloud9 I run the code

import boto3

s3 = boto3.client('s3')
s3.create_bucket(Bucket='cst-my-bucket')

and get the error message:

Traceback (most recent call last):
File "/home/ec2-user/environment/serverless/s3/create.py", line 4, in <module>
s3.create_bucket(Bucket='cst-my-bucket')
File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

Process exited with code: 0

works fine when run on local jupyter notebook

gefragt vor 5 Jahren15986 Aufrufe
2 Antworten
1

Hi,
By default, the boto3 create_bucket call (with just the bucket name as the parameter) will attempt to create the bucket in the default "us-east-1" region.
You will need to add the following to your boto3 create bucket call:

CreateBucketConfiguration={'LocationConstraint': '<same region as your specified default>'})

You most likely followed the directions to setup your credentials and specified the AWS_DEFAULT_REGION as something other than "us-east-1".
Link: https://docs.aws.amazon.com/cloud9/latest/user-guide/credentials.html

For example, if you set your default location to "us-east-2" and then ran

import boto3
s3 = boto3.client('s3')
s3.create_bucket(Bucket='mybucket-sdfslkjd')

You would get the error:

Traceback (most recent call last):
  File "/home/ec2-user/environment/createBucket.py", line 4, in <module>
    s3.create_bucket(Bucket='lddidddddd')
  File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

Now if you add the LocationConstraint to the create-bucket call to explicitly create the bucket in the "us-east-2" region, it would work properly.

import boto3
s3 = boto3.client('s3')
s3.create_bucket(Bucket='lddidddddd',
    CreateBucketConfiguration={'LocationConstraint': 'us-east-2'})

Process exited with code: 0
And the bucket gets created in region us-east-2.

Hope this helps!
-randy

beantwortet vor 5 Jahren
0

Many thanks randy! That fixed the problem and your explanation made sense

cheers
Jon

beantwortet vor 5 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen