Google CloudGoogle Cloud IAMgoogle_service_account_keyPro

How to Import an Google Service Account Key into Terraform

To import an existing Google Service Account Key into Terraform, scan it with Terraback and run terraback gcp import --method bulk. Terraback writes the matching google_service_account_key resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the full key name projects/{project}/serviceAccounts/{account_email}/keys/{key_id} (note: the Terraform provider does not support importing this resource because the private key material is only returned at creation).

Import Google Service Account Key with Terraback (recommended)

Terraback reverse-engineers your live infrastructure: it reads the Google Cloud IAM resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live Google Service Account Key to managed Terraform.

1

Scan your Google Cloud account

terraback scan all gcp --project my-gcp-project
2

Generate import blocks and import into state

terraback gcp import --method bulk

The Terraform import block

Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.

import {
  to = google_service_account_key.deploy
  id = "projects/my-project/serviceAccounts/deploy@my-project.iam.gserviceaccount.com/keys/abc123..."
}

Example google_service_account_key configuration

Here is a realistic Google Service Account Key block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.

resource "google_service_account_key" "deploy" {
  service_account_id = google_service_account.deploy.name
  public_key_type    = "TYPE_X509_PEM_FILE"
}

Gotchas when importing a Google Service Account Key

  • The Terraform google provider does not support importing google_service_account_key: the private key is only ever returned at creation, so an existing key cannot be brought under management and must be recreated instead.
  • The generated private_key is a highly sensitive value stored in plaintext in Terraform state; treat the whole state file as a secret and prefer Workload Identity over keys where possible.
  • service_account_id should be the service account name or unique ID (for example google_service_account.deploy.name), and that parent service account is a separate resource.
  • Recreating this resource rotates the key, immediately invalidating the previous credentials that any external systems were using.

Doing it manually with terraform import

The native approach is to write the google_service_account_key block by hand, then run terraform import google_service_account_key.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.

Import your whole Google Cloud account in minutes

Terraback scans 80+ Google Cloud resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.