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

asked 5 years ago15710 views
2 Answers
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

answered 5 years ago
0

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

cheers
Jon

answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions