How to fix Error importing resource in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 30, 2025· 88 views

Terraform import fails when credentials aren't configured locally. Unlike other Terraform commands that run in the cloud, terraform import executes on your workstation and requires local authentication to access cloud resources.

Quick fix

Fixes Error importing resource

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.0"
    }
  }
}

provider "google" {
  project = "my-project"
  region  = "us-central1"
  # Either option:
  # credentials = file("/path/to/service-account-key.json")
  # OR
  # access_token = var.gcp_access_token
}
  1. Verify Terraform provider is configured with credentials
  2. Authenticate with gcloud auth application-default login
  3. Or, set GOOGLE_APPLICATION_CREDENTIALS environment variable
  4. Create the resource block in your configuration before importing
terraform — error
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}
provider "google" {
project = "my-project"
region = "us-central1"
# Either option:
# credentials = file("/path/to/service-account-key.json")
# OR
# access_token = var.gcp_access_token
}
Error importing resource

What this error means

The terraform import command establishes a connection between existing cloud resources and Terraform state. This error occurs because the import command runs locally on your machine, unlike apply or plan which execute in Terraform Cloud/Enterprise. Your local environment lacks the credentials needed to authenticate with the cloud provider API.

Typical symptoms

  • terraform import command fails with authentication error
  • Error message mentions "application default credentials" or missing provider configuration
  • Import fails locally but terraform apply works in Terraform Cloud
  • Error occurs when importing GCP, AWS, or Azure resources

Common causes

  1. No credentials configured in the local Terraform provider block
  2. GOOGLE_APPLICATION_CREDENTIALS or equivalent environment variable not set
  3. gcloud auth application-default login not run on workstation
  4. Running terraform import from Terraform Cloud/Enterprise (assumes remote credentials will be available, but terraform import always runs locally)
  5. Service account key file not accessible or invalid
  6. Missing required IAM permissions on the service account

How to fix "Error importing resource"

1Verify Terraform provider is configured with credentials

Check your provider block in your Terraform configuration. For Google Cloud, it should include either credentials or access_token:

hcl
terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.0"
    }
  }
}

provider "google" {
  project = "my-project"
  region  = "us-central1"
  # Either option:
  # credentials = file("/path/to/service-account-key.json")
  # OR
  # access_token = var.gcp_access_token
}

If credentials aren't set, continue to step 2.

2Authenticate with gcloud auth application-default login

For local development with Google Cloud, run:

bash
gcloud auth application-default login

This creates local credentials that Terraform will use automatically. A browser window will open for authentication. Approve the permissions to generate the credentials file at ~/.config/gcloud/application_default_credentials.json.

3Or, set GOOGLE_APPLICATION_CREDENTIALS environment variable

If you have a service account key file (JSON), export it as an environment variable:

bash
# On Linux/macOS
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"

# On Windows PowerShell
$env:GOOGLE_APPLICATION_CREDENTIALS = "C:\path\to\service-account-key.json"

Verify the path is correct and the file exists:

bash
cat $GOOGLE_APPLICATION_CREDENTIALS | jq .
4Create the resource block in your configuration before importing

Before running terraform import, define an empty resource block for the resource you're importing:

hcl
resource "google_compute_instance" "example" {
  # Empty - will be populated by import
}

This gives Terraform a place to store the imported state.

5Run terraform import with the correct resource ID format

Different resource types use different ID formats. Check the provider documentation for the correct format:

bash
# Google Compute Instance: zone/instance-name
terraform import google_compute_instance.example us-central1-a/my-instance

# Google Cloud Storage Bucket: bucket-name
terraform import google_storage_bucket.example my-bucket

# Google SQL Instance: project:instance-name
terraform import google_sql_database_instance.example my-project:my-instance

Refer to the Terraform Google Provider documentation for the specific ID format for your resource type.

6Verify the import succeeded

After running terraform import, check that the state was updated:

bash
terraform state show google_compute_instance.example

You should see the imported resource's attributes. If the import succeeded but attributes are missing, you may need to add them to your configuration.

Advanced notes

If using Terraform Cloud or Terraform Enterprise, remember that terraform import always executes locally, not in the remote environment. You must set up credentials on your local workstation first. For CI/CD pipelines, store the service account key or access token as a secure environment variable in your pipeline platform. Never commit credentials to version control. For multi-account setups, use Terraform variables or separate provider aliases to manage different credentials for different resources. Consider using workload identity federation (available for GCP) to avoid storing long-lived service account keys entirely.

Frequently asked questions

What does "Error importing resource" mean?
The terraform import command establishes a connection between existing cloud resources and Terraform state. This error occurs because the import command runs locally on your machine, unlike apply or plan which execute in Terraform Cloud/Enterprise. Your local environment lacks the credentials needed to authenticate with the cloud provider API.
How do I fix "Error importing resource"?
Terraform import fails when credentials aren't configured locally. Unlike other Terraform commands that run in the cloud, terraform import executes on your workstation and requires local authentication to access cloud resources.
What causes "Error importing resource"?
No credentials configured in the local Terraform provider block GOOGLE_APPLICATION_CREDENTIALS or equivalent environment variable not set gcloud auth application-default login not run on workstation
Is the "Error importing resource" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading