diff --git a/backend/remote-state/gcs/backend.go b/backend/remote-state/gcs/backend.go index d32a166e86..ae69fb9945 100644 --- a/backend/remote-state/gcs/backend.go +++ b/backend/remote-state/gcs/backend.go @@ -28,6 +28,7 @@ type gcsBackend struct { defaultStateFile string projectID string + region string } func New() backend.Backend { @@ -67,6 +68,13 @@ func New() backend.Backend { Description: "Google Cloud Project ID", Default: "", }, + + "region": { + Type: schema.TypeString, + Optional: true, + Description: "Region / location in which to create the bucket", + Default: "", + }, }, } @@ -95,6 +103,10 @@ func (b *gcsBackend) configure(ctx context.Context) error { if id := os.Getenv("GOOGLE_PROJECT"); b.projectID == "" && id != "" { b.projectID = id } + b.region = data.Get("region").(string) + if r := os.Getenv("GOOGLE_REGION"); b.projectID == "" && r != "" { + b.region = r + } opts := []option.ClientOption{ option.WithScopes(storage.ScopeReadWrite), @@ -128,6 +140,7 @@ func (b *gcsBackend) ensureBucketExists() error { attrs := &storage.BucketAttrs{ VersioningEnabled: true, + Location: b.region, } return b.storageClient.Bucket(b.bucketName).Create(b.storageContext, b.projectID, attrs) diff --git a/website/docs/backends/types/gcs.html.md b/website/docs/backends/types/gcs.html.md index 5f55a1f8e9..90d8d94057 100644 --- a/website/docs/backends/types/gcs.html.md +++ b/website/docs/backends/types/gcs.html.md @@ -57,3 +57,5 @@ The following configuration options are supported: * `path` - (Deprecated) GCS path to the state file of the default state. For backwards compatibility only, use `prefix` instead. * `project` / `GOOGLE_PROJECT` - (Optional) The project ID to which the bucket belongs. This is only used when creating a new bucket during initialization. Since buckets have globally unique names, the project ID is not required to access the bucket during normal operation. + * `region` / `GOOGLE_REGION` - (Optional) The region in which a new bucket is created. + For more information, see [Bucket Locations](https://cloud.google.com/storage/docs/bucket-locations).