Search code examples
amazon-web-servicesamazon-s3aws-cloudformationaws-cloudformation-custom-resource

IllegalLocationConstraintException whilst creating an S3 bucket in CFN resource provider test


I'm creating a Cloudformation Resource Provider using the cloudformation cli. When I try to test my resource locally using cfn test I get the following error:

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

My code looks as follows:

s3 = session.client("s3")
s3.create_bucket(Bucket='mybucket123',CreateBucketConfiguration={'LocationConstraint': 'us-east-2'})

I've also tried other regions and have tried without the LocationConstraint and I get the same error with the word undefined where the region code is:

s3 = session.client("s3")
s3.create_bucket(Bucket='mybucket123')

cfn test actually runs the code within sam local using sam local start-lambda so I'm a bit perplexed as to why I would even need a region here?

Any ideas?


Solution

  • Your client must also be set to the region you want:

    s3 = session.client("s3", region_name='us-east-2')
    s3.create_bucket(Bucket='mybucket123',CreateBucketConfiguration={'LocationConstraint': 'us-east-2'})