boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
9.09k stars 1.87k forks source link

s3.create_bucket() fails if the region is set to 'ap-northeast-2' #781

Closed hoon closed 3 years ago

hoon commented 8 years ago

When I try to create an S3 bucket on 'ap-northeast-2' using boto3, I get an IllegalLocationConstraintException:

>>> import boto3
>>> s3 = boto3.resource('s3')
>>> s3.create_bucket(Bucket='this-will-fail')
Traceback (most recent call last):
[default]
  File "<stdin>", line 1, in <module>
  File "/Users/hoon/my-venv/lib/python3.5/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/Users/hoon/my-venv/lib/python3.5/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/Users/hoon/my-venv/lib/python3.5/site-packages/botocore/client.py", line 159, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/hoon/my-venv/lib/python3.5/site-packages/botocore/client.py", line 494, in _make_api_call
    raise ClientError(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.

My .aws/config is as follows

[default]
region = ap-northeast-2

If I change the default region in .aws/config to 'ap-northeast-1', create_bucket() seems to work:

>>> s3.create_bucket(Bucket='this-will-work')
s3.Bucket(name='this-will-work')

But, when I go into the AWS web console, I see that 'this-will-work' bucket was created in 'US Standard,' not 'ap-northeast-1'.

It's possible to get around all these problems and create a bucket in 'ap-northeast-2' region by doing this:

s3.create_bucket(Bucket='workaround', CreateBucketConfiguration={'LocationConstraint': 'ap-northeast-2'})

and setting .aws/config default region to 'ap-northeast-2'. (If it's set to anything else, the above workaround will fail with IllegalLocationConstraintException.

In summary, boto3 seems to be exhibiting an odd behaviour where:

  1. It fails to create S3 bucket if the region is set to 'ap-northeast-2' in .aws/config
  2. It ignores .aws/config and creates bucket in 'US Standard' if the region is set to 'ap-northeast-1'

The version of boto3 I'm using is 1.4.0.

jamesls commented 8 years ago

There's potential to improve this, but I wanted to explain the error's your seeing, and at least why it's the current expected behavior.

The improvement we can make is to automatically inject the CreateBucketConfiguration parameter on your behalf, with the correct values, if you don't provide one, based on the region you have configured. That way you'll always create a bucket in whatever region you're client is configured for.

The main area of concern is the fact that this is a change in default behavior and we'd need to make sure no one would be adversely affected. The error -> now working case should be fine, but there's concern if anyone is relying on the behavior for creating a bucket in US Standard because they didn't specify a CreateBucketConfiguration despite the fact that they might have us-west-2 configured.

Going to label this as needs-discussion for the time being.

cjcjameson commented 7 years ago

There is also a special case regarding .s in the desired bucket name. I've put more information on https://github.com/aws/aws-cli/issues/589 -- should I open up a new issue?

Also overlaps with #627 maybe

venu6644 commented 5 years ago

I get this error when I do Put Bucket encryption in my lambda script

An error occurred (IllegalLocationConstraintException) when calling the PutBucketEncryption operation: The ap-east-1 location constraint is incompatible for the region specific endpoint this request was sent to.: ClientError

github-actions[bot] commented 4 years ago

Greetings! It looks like this issue hasn’t been active in longer than one year. We encourage you to check if this is still an issue in the latest release. Because it has been longer than one year since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment to prevent automatic closure, or if the issue is already closed, please feel free to reopen it.

github-actions[bot] commented 3 years ago

Greetings! It looks like this issue hasn’t been active in longer than one year. We encourage you to check if this is still an issue in the latest release. Because it has been longer than one year since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment to prevent automatic closure, or if the issue is already closed, please feel free to reopen it.