Quick Fix for Location Constraints for S3 CreateBucket API
This short post is not meant to elaborate on what Amazon S3 is or how and what it is used for but to offer a quick fix to the error shown below:
An error occurred (IllegalLocationConstraintException) when calling the CreateBucket Operation: The unspecified location constraint is incompatible for the region specific endpoint this request sent to.
To learn about Amazon S3 in detail, review the following posts:
There is something to pay attention to when calling the CreateBucket API as you would likely encounter the 'IllegalLocationConstraintException' error when creating S3 Bucket with the command below:
aws s3api create-bucket <bucketname>
Why the IllegalLocationConstraintException Error?
This error occurs when creating an S3 bucket without specifying a location constraint in a region other than us-east-1. The AWS S3 API requires that you specify a location constraint for regions other than the default us-east-1.
The Quick Fix
Now, to create an S3 bucket in a specific region, you need to specify the --create-bucket-configuration parameter with the appropriate location constraint.
Here is how you can create an S3 bucket in the eu-west-1 region:
aws s3api create-bucket --bucket <bucketname> --create-bucket-configuration LocationConstraint=eu-west-1
The below screenshot shows the error I encountered and how it was resolved by adding the --create-bucket-configuration flag.
Why did I need an S3? ??
So I was working on a Terraform project and required an Amazon S3 bucket to store the State file securely.
Important Note:
If you have not defined your default region as the "us-east-1" region, you will require the --region eu-west-1 parameter in the command. See below:
aws s3api create-bucket --bucket <bucketname> --region eu-west-1 --create-bucket-configuration LocationConstraint=eu-west-1
Command Explanation
Let me do justice by explaining the commands:
Did you find this helpful?