Where Is the Key?
resource "google_service_account" "myaccount" {
  account_id = "dev-foo-account"
}

resource "google_service_account_key" "mykey" {
  service_account_id = google_service_account.myaccount.name
}

data "google_service_account_key" "mykey" {
  name            = google_service_account_key.mykey.name
  public_key_type = "TYPE_X509_PEM_FILE"
}

If I create a Service Account and a key like this - how do I obtain the key afterwards?

terraform output yields:

$ terraform output -json google_service_account_key
The output variable requested could not be found in the state
file. If you recently added this to your configuration, be
sure to run `terraform apply`, since the state won't be updated
with new output variables until that command is run.

1 Answer

You have to put that variable as an output if you want to use it after apply the plan:


output "my_private_key" {
  value = data.google_service_account_key.mykey.private_key
}

To output the value of "my_private_key":

$ terraform output my_private_key

To obtain the credentials as a JSON which can later be used for authentication:

$ terraform output -raw key | base64 -d -
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest