terraform-provider-aws icon indicating copy to clipboard operation
terraform-provider-aws copied to clipboard

Add "Region" as argument reference for Data Source: aws_s3_bucket

Open GouravIN opened this issue 2 years ago • 5 comments

It would be nice if we can have region reference when we are looking for a S3 bucket in data source. If the S3 bucket is in another region and your provider is defined for a different region, then getting the following error.

In my case: S3 bucket is created in: us-east-1

and provider has region = "us-west-2". I wanted to get the ARN of the bucket to get and use in the current terraform definition.

My Error:

data "aws_s3_bucket" "openvpn_bucket" { bucket = "seceng-terraform-statefiles" }

Error:

╷ │ Error: Failed getting S3 bucket (seceng-terraform-statefiles): BucketRegionError: incorrect region, the bucket is not in 'us-west-2' region at endpoint '' │ status code: 301, request id: , host id: │ │ with data.aws_s3_bucket.openvpn_bucket, │ on data.tf line 15, in data "aws_s3_bucket" "openvpn_bucket": │ 15: data "aws_s3_bucket" "openvpn_bucket" { │ ╵

GouravIN avatar Jul 14 '22 16:07 GouravIN

Hey @GouravIN 👋 Thank you for taking the time to raise this! One option to achieve this functionality with the provider today would be to use a provider alias to allow you to access the separate region. The configuration would look something like the below configuration. Would this satisfy your needs?

provider "aws" {
  alias = "usw2"
  region = "us-west-2"
}

data "aws_s3_bucket" "openvpn_bucket" {
  provider = aws.usw1
  bucket = "seceng-terraform-statefiles"
}

justinretzolk avatar Jul 21 '22 15:07 justinretzolk

The suggestion will not work in a module with for_each. Allowing a data source to query another region would be wonderful.

xsnrg avatar Dec 19 '22 17:12 xsnrg

How about if we are trying to discover the region at deploy time and don't know it so can't create a provider ahead of time,

Samrose-Ahmed avatar Sep 19 '23 18:09 Samrose-Ahmed

How about if we are trying to discover the region at deploy time and don't know it so can't create a provider ahead of time,

oh snap.. seriously how is this not a thing? Orchestrating across regions, accounts, etc is not an edge case.

christian-elsee avatar Nov 03 '23 08:11 christian-elsee

this feature shouldnt just be for aws, all clouds need this, especially google cloud and ibm, which dont have region restricted global services or vpcs...

65156 avatar Feb 22 '24 23:02 65156