Introduction
When importing a resource using Terraform the following error occurs:
Error: Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. Original error: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
Problem
$ terraform import google_project.project_name test-project-123456
Acquiring state lock. This may take a few moments...
Error: Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. Original error: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
on <empty> line 0:
(source code not available)
Error: Invalid provider configuration
on provider.tf line 1:
1: provider "google" {
The configuration for provider["registry.terraform.io/hashicorp/google"]
depends on values that cannot be determined until apply.
Cause
When you use Terraform on the command line with Terraform Cloud or Terraform Enterprise, many commands like terraform apply
run inside your Terraform Cloud or Terraform Enterprise environment. However, the terraform import
command runs locally, so it does not have access to information from the Terraform Cloud or Terraform Enterprise workspace. To successfully perform an import, you will need to set local variables equivalent to any remote workspace variables in Terraform Cloud or Terraform Enterprise.
Solution:
Authenticate with Google Cloud. Local credentials can be set via the following methods:
-
gcloud auth application-default login
Outcome
Once the credentials are set locally, run the terraform import process again.