Raffael
Raffael

Reputation: 20045

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.

Upvotes: 1

Views: 2777

Answers (1)

Jaime S
Jaime S

Reputation: 1698

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 -

Upvotes: 10

Related Questions