terraform-provider-aws: s3: BucketRegionError: incorrect region, the bucket is not in
Community Note
- Please vote on this issue by adding a š reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave ā+1ā or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
Terraform CLI and Terraform AWS Provider Version
$ terraform -v
Terraform v1.1.5
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.1.0
Affected Resource(s)
- aws_s3_bucket_acl
Terraform Configuration Files
resource "aws_s3_bucket_acl" "example" {
bucket = aws_s3_bucket.example.id
acl = "private"
}
Debug Output
Error: error getting S3 bucket ACL (backup,private): BucketRegionError: incorrect region, the bucket is not in 'ap-southeast-2' region at endpoint '', bucket is in 'ap-northeast-2' region
ā status code: 301
Expected Behavior
I migrated the code to use the new aws_s3_bucket_acl resource (since I previously had the acl=āprivateā parameter set but this fails with the 1.1.x release), according to the documentation from here, however when I import the resource into Terraform, for some reason fails and tells me the bucket is on a different region (which is not since I confirmed the bucket has been created on the region āap-southeast-2ā, not in Korea. Having said that, everything was working well until someone upgraded to the latest release.
Importing the resource should work.
Actual Behavior
It does mention an endpoint that is not used and doesnāt add the resource to the state file.
Steps to Reproduce
terraform init --upgradeterraform import aws_s3_bucket_acl.example example,private
References
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 14
- Comments: 23 (9 by maintainers)
Cause
Locationheader. (Currently, the API returns noLocationheader and v2 fills inLocation: https://amazonaws.com/badhttpredirection.)Locationheader. S3 does returnX-Amz-Bucket-Region: ap-northeast-2header but this seems to be the incorrect location.Although this error seems to be new to you, it has been around for a while with #14544. Sometimes the problem was fixed by
rm -rf .terraformand re-initializing,terraform init. Other times it was not.Solutions
Unfortunately, we donāt have a clear smoking gun and clear path forward. However, we have some ideas that may or may not help.
Idea 1
The AWS provider v4.2 will include HTTP client and transport changes. However, the change will not be dramatic so this is maybe only be a 50/50 or less chance of helping.
Idea 2
As mentioned above, others have found that deleting
.terraformdirectory fixed the problem. The op mentions that this did not fix the problem for him.Idea 3
Itās possible that you can use a config workaround to avoid the 301 response. This is basically saying, āOkay, AWS. Iāll play your silly game. The bucket is in X region.ā You would only add the
providerargument to the problematic resource.Unfortunately, since we have not been able to reproduce the problem, we cannot test this idea.
Idea 4
This may be the only true solution even if it is not very satisfying. We recommend that you reach out to AWS Support and raise the problem for the specific bucket. Although this worked before, we have found many times that things accidentally worked before that ābreakā as we upgrade different components. But, they really shouldnāt have worked before.
Locationheader. We cannot change that response.Location: https://amazonaws.com/badhttpredirectlocation.aws s3api get-bucket-location --bucket yourbucket) gives one result but the API itself is saying that the bucket is in a different region with theX-Amz-Bucket-RegionHTTP response header.Raise these specific issues with AWS Support and see if they can adjust something with the bucket to fix the problem. S3 used to work very differently, and it is possible that through the various upgrades and migrations, some buckets were missed.
After some testing, it appears adding
provider = aws.us-west-1to each new resource solved my issue.Idea 3 worked for me!
I figured out my error, and itās an ID10T error.
Iāve made a gist that encapsulates a full test suite on this that can be used by the use of
terraform.tfvars(gitignored in the gist). https://gist.github.com/halostatue/cf1ec2a93a455815813ac51775b13da4The main point in the
Makefilecan be shown that I was doing the import incorrectly (targetimport-wrong):When I imported correctly, everything started working (target
import-right):The error that I am seeing is definitely user error; it may not be the same issue as @korporationcl. I think that thereās still a bug in that we should be getting a āno such bucketā sort of error instead of a ābad regionā error.
I also think that thereās documentation improvement that could be done, because the example on the upgrade documentation for imports uses
bucket = "bucket", which means thatterraform import aws_s3_bucket_acl.bucket bucket, privatemakes sense, but if the documentation usedbucket = "example-bucket", then it would be clearer that the resource name is not the same.I know this from the few times that I have done resource importsā¦but dealing with imports properly was the last thing on my mind as I was having to deal with an unplanned major upgrade when moving to AWS 4.x.
Sorry for the wild goose chase on my end, but I do think there are bugs hereā¦just not what I was seeing.
Hey guys - this is also happening with me and my bucket is āonlyā 10 months old. The bucket region is
eu-west-2and I am getting exactly the same error as the op. Let me know if I can help testing this any further.