Nick's Blog

Error configuring the backend “gcs”: storage.NewClient() failed: dialing: google: could not find default credentials

I’m setting up terraform with google cloud storage backend. when I run “terraform init”, I was getting the following error:

Initializing the backend...

Error configuring the backend "gcs": storage.NewClient() failed: dialing: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

Please update the configuration in your Terraform files to fix this error
then run this command again.

The following was my backend.tf and provider.tf file

backend.tf
terraform {
  backend "gcs" {
    bucket      = "nk-terraform-state"
    prefix      = "terraform/state"
  }
}

provider.tf
provider "google" {
  credentials = "5d0fa492f8e0.json"
  project     = "kiwiops-test-nick"
  region      = "australia-southeast1"
}

I thought this would work fine. however, after the investigation. I figured out that terraform module will always run first, which means even though I have credential configured from my provider.tf. I will still not able to reference it from the very beginning.

So what I did is just add the credentials into my backend.tf file. and it passed the terraform init.

terraform {
  backend "gcs" {
    credentials = "5d0fa492f8e0.json"
    bucket      = "nk-terraform-state"
    prefix      = "terraform/state"
  }
}

Since I have already called credentials from my terraform module, I dont really need to provide it again in the provider.tf file.