Data source `aws_s3_bucket` errors due to 301 "Moved Permanently"

I have a data source:

data "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
}

terraform plan is failing with:

Error: reading S3 Bucket (): operation error S3: HeadBucket, https response error StatusCode: 301, RequestID: XXX, HostID: XXXXX=, api error MovedPermanently: Moved Permanently

I googled around and found a useful SO reply:

This is a frequently encountered issue: You are trying to access a bucket in region us-west-1, however, for legacy reasons the default Amazon S3 region in most/all AWS SDKs is US Standard, which automatically routes requests to facilities in Northern Virginia or the Pacific Northwest using network maps (see Regions and Endpoints for details).

Therefore you simply need to specify the endpoint of your buckets region explicitly

My takeaway from that comment is that I need to specify the bucket’s region however data source aws_s3_bucket doesn’t take a region argument. So I’m unclear on what I can do to fix this.

Any suggestions?

OK, think I’ve figured it out, I need to have a separate provider configuration

provider "aws" {
  alias = "other"
  region = "ap-southeast-2"
}

then refer to that like:

data "aws_s3_bucket" "bucket" {
  provider = aws.other
  bucket = var.bucket_name
}