23

I was using terraform in cloud build, but it fails at this step

steps:
  # Terraform
  - id: 'configure_terraform'
    name: node:10.16.3
    entrypoint: "node"
    args: ["./create_terraform_config.js",
           "../terraform/override.tf",
           "${_TERRAFORM_BUCKET_NAME}",
           "${_TERRAFORM_BUCKET_PATH}"]
    dir: "app/scripts"
  - id: 'init_terraform'
    name: hashicorp/terraform:light
    args: ["init"]
    dir: "app/terraform"
Initializing the backend...

Successfully configured the backend "gcs"! Terraform will automatically
use this backend unless the backend configuration changes.

Error: Failed to get existing workspaces: querying Cloud Storage failed: storage: bucket doesn't exist
giampaolo
  • 6,906
  • 5
  • 45
  • 73
Elona Mishmika
  • 480
  • 2
  • 5
  • 21

4 Answers4

36

This might fix the issue

terraform init -reconfigure

reference: https://github.com/hashicorp/terraform/issues/23532#issuecomment-560493391

Greg
  • 1,845
  • 2
  • 16
  • 26
madhu reddy
  • 464
  • 4
  • 7
  • 2
    In my case, `terraform init -reconfigure` produced the same error. I was able to resolve my issue via [@Oliver](https://stackoverflow.com/a/72382324/3542457)'s solution of changing the [application-default login](https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login). – Greg Feb 04 '23 at 03:40
23

This is what worked for me:

gcloud auth application-default login --project $PROJECT

Normally I omit the --project arg but that still generated the error. I thought it was only used for billing/quota. This may be a bug related to my specific tool versions:

Google Cloud SDK 387.0.0
Terraform v1.1.2
Oliver
  • 421
  • 3
  • 6
  • This should be the accepted answer, detail in [terraofrm's docs](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started#adding-credentials). A bit more detail can be found [here](https://stackoverflow.com/a/57455958/799379) – Lockszmith Jun 21 '23 at 14:42
  • This worked for me too. Selecting a project worked for me. – Promise Preston Aug 25 '23 at 09:03
2
  1. State bucket must be pre-existing. GCS backend bucket must pre-exist

If it doesn't exist, create the state bucket and version it, using the following commands:

gsutil mb -p <projectId> -c <storage-class> -l <region> -b gs://<bucket-name>
gsutil versioning set on gs://<bucket-name>
  1. As specified in the above answer execute terraform init with -reconfigure option.
Nandan
  • 497
  • 2
  • 7
  • 18
0

This can happen if you have previously exported the GOOGLE_APPLICATION_CREDENTIALS var in your current terminal.

To check that you can run gcloud auth application-default login. You will have the following output:

The environment variable [GOOGLE_APPLICATION_CREDENTIALS] is set to:
  [/home/alphayax/gcp/xxxxxxxxx-123456789abc.json]
Credentials will still be generated to the default location:
  [/home/alphayax/.config/gcloud/application_default_credentials.json]
To use these credentials, unset this environment variable before
running your application.

So you just have to unset the env var with the following:

export GOOGLE_APPLICATION_CREDENTIALS=
alphayax
  • 2,930
  • 2
  • 25
  • 25