Terraform: fixing error “querying Cloud Storage failed: storage: bucket doesn’t exist”

If you are attempting to run “terraform init” with a Google Cloud Storage backend and get the following error:

Error: Failed to get existing workspaces: querying Cloud Storage failed: storage: bucket doesn't exist

The first check should be that the Google Cloud Storage bucket indeed exists, using gsutil.

project_id=myproject-123
gsutil ls -p $project_id

If the GCS bucket does exist and your terraform Google provider is relying on ‘gcloud auth application-default’ for credentials (versus an explicit account), then make sure the login context is set to the expected GCP project using the command below.

gcloud auth application-default login --project $project_id

Because the error message can be indicative of the login context (in well-known location) being set to an older or unexpected GCP project.

cat ~/.config/gcloud/application_default_credentials.json

 

REFERENCES

google, how ADC works and well-known locations

gcloud auth application-default

devcoops.com, ways to deal with ‘querying Cloud Storage failed’

gsutil install

fabianlee.org, creating GCS bucket for use by Terraform