Unable to parse credentials for Intro to Terraform on GCP LinkedIn Learning course

I have a fork of this repo GitHub - LinkedInLearning/introduction-to-terraform-on-google-cloud-platform-4506001: This is a repository for the LinkedIn Learning course Introduction to Terraform on Google Cloud Platform which I’ve updated to include my gcp project ID. I have the GOOGLE_CREDENTIALS variable set containing the key file I generated from creating a key in GCP. I’m getting the unable to parse credentials error when trying to run within terraform cloud.
more info:
Terraform v1.8.5
on linux_amd64
Initializing plugins and modules…
{“@level”:“info”,“@message”:“Terraform 1.8.5”,“@module”:“terraform.ui”,“@timestamp”:“2024-06-10T16:58:55.325407Z”,“terraform”:“1.8.5”,“type”:“version”,“ui”:“1.2”}
{“@level”:“info”,“@message”:“Plan: 0 to add, 0 to change, 0 to destroy.”,“@module”:“terraform.ui”,“@timestamp”:“2024-06-10T16:58:56.469577Z”,“changes”:{“add”:0,“change”:0,“import”:0,“remove”:0,“operation”:“plan”},“type”:“change_summary”}
{“@level”:“error”,“@message”:“Error: unable to parse credentials”,“@module”:“terraform.ui”,“@timestamp”:“2024-06-10T16:58:56.469855Z”,“diagnostic”:{“severity”:“error”,“summary”:“unable to parse credentials”,“detail”:“invalid character ‘"’ after object key:value pair”,“address”:“provider["registry.terraform.io/hashicorp/google"]”,“range”:{“filename”:“providers.tf”,“start”:{“line”:10,“column”:19,“byte”:140},“end”:{“line”:10,“column”:20,“byte”:141}},“snippet”:{“context”:“provider "google"”,“code”:“provider "google" {”,“start_line”:10,“highlight_start_offset”:18,“highlight_end_offset”:19,“values”:}},“type”:“diagnostic”}
Operation failed: failed running terraform plan (exit 1)

any ideas what could be wrong?

Hi @hoevans87

The below is your message from above (When posting code or errors please consider using the code block formatting using ``` before and after the code).

{“@level”:“info”,“@message”:“Terraform 1.8.5”,“@module”:“terraform.ui”,“@timestamp”:“2024-06-10T16:58:55.325407Z”,“terraform”:“1.8.5”,“type”:“version”,“ui”:“1.2”}
{“@level”:“info”,“@message”:“Plan: 0 to add, 0 to change, 0 to destroy.”,“@module”:“terraform.ui”,“@timestamp”:“2024-06-10T16:58:56.469577Z”,“changes”:{“add”:0,“change”:0,“import”:0,“remove”:0,“operation”:“plan”},“type”:“change_summary”}
{“@level”:“error”,“@message”:“Error: unable to parse credentials”,“@module”:“terraform.ui”,“@timestamp”:“2024-06-10T16:58:56.469855Z”,“diagnostic”:{“severity”:“error”,“summary”:“unable to parse credentials”,“detail”:“invalid character ‘"’ after object key:value pair”,“address”:“provider["registry.terraform.io/hashicorp/google"]”,“range”:{“filename”:“providers.tf”,“start”:{“line”:10,“column”:19,“byte”:140},“end”:{“line”:10,“column”:20,“byte”:141}},“snippet”:{“context”:“provider "google"”,“code”:“provider "google" {”,“start_line”:10,“highlight_start_offset”:18,“highlight_end_offset”:19,“values”:}},“type”:“diagnostic”}

The 3rd line appears to be directing you to the exact issue:

"summary":"unable to parse credentials",
"detail":"invalid character '"' after object key:value pair"
"filename":"providers.tf"
"start":{"line":10,"column":19,"byte":140}
"end":{"line":10,"column":20,"byte":141}

Check your formatting of your providers.tf file, specifically line 10. It appears to not be formatted correctly. Either with unneeded " characters or perhaps you are missing something else which is then causing the parser to see the ‘"’ as unexpected at that point.

Hope that helps

Happy Terraforming

Thanks for the reply, if you look at the repo I linked, that file is only 5 lines of code so doesn’t seem that simple. I’ve tried complete retyping that file and still get the same result when I push to my forked main branch.

provider "google" {
  project = "terraform-training-425614"
  region  = "us-west1"
  zone    = "us-west1-a"
}

From the terraform run page the error denoted that it’s on line 1 but not sure what could be wrong here. This seems like pretty basic code formatting that it is having trouble with reading.

Apologies, I did not take the time to read your original post fully.

In this case the error is somewhat misleading insofar that the error message refers to the provider block, however the issue here is likely related to the contents of your HCP workspace’s GOOGLE_CREDENTIALS variable. The content is in some way malformed. You should re-set the content of that variable from your .json key file.

Note that the actual error description invalid character '"' after object key:value pair is relevant to the error that HCP is seeing when trying to use the content of the variable.

I was able to reproduce your error by adding a spurious " at the end of one of the lines, whereas other common errors such as omitting the opening or closing { in the json will return different errors such as
invalid character ':' after top-level value
or
unexpected end of JSON input

Hope that helps

Happy Terraforming

Thanks again for another reply, I had saw something suggesting this as well but I see no erroneous " within what I’m copying and pasting in the GOOGLE_CREDENTIALS value field. Obfuscated version here:

{ "type": "service_account" "project_id": "terraform-training-425614" "private_key_id": "" "private_key": "-----BEGIN PRIVATE KEY-----\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-----END PRIVATE KEY-----\n" "client_email": "terraform@terraform-training-425614.iam.gserviceaccount.com" "client_id": "" "auth_uri": "https://accounts.google.com/o/oauth2/auth" "token_uri": "https://oauth2.googleapis.com/token" "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs" "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/terraform%40terraform-training-425614.iam.gserviceaccount.com" "universe_domain": "googleapis.com" }

I did however discover that if trying to echo that into my terminal, which I’m sure isn’t apples to apples with how terraform is reading but figured it wouldn’t hurt to try, that it fails to parse around `}’ which is the ending character yet I see no issues here. got it to properly return the response in terminal but still failed in terraform with the same error.

OK - if that is exactly (excepting the obfuscation) what you have in your variable it is invalid JSON.

Each "key" : value pair in the JSON object should be separated by a ,

Here is the json I input into my variable. Note the , at the end of each line within the { } object:

{
  "type": "service_account",
  "project_id": "discuss-67731",
  "private_key_id": "1cc8be59fe46a55a8e2c09059dd61de2685d4bb7",
  "private_key": "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n",
  "client_email": "tfc-308@discuss-67731.iam.gserviceaccount.com",
  "client_id": "12345",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/tfc-308%40discuss-67731.iam.gserviceaccount.com",
  "universe_domain": "googleapis.com"
}

These , should remain, even if you are removing the trailing newline characters as described in the provider documentation.

Happy Terraforming

Thank you!! That worked.

Must have been some update on terraform. Was working without commas in the training video.

Also, I did check that page but I saw that the code under the purple ‘use provider’ button towards the top right of the page and that code doesn’t have commas either.

Would be great if that reflected the working code.

thanks again!

Hi @hoevans87,

I’m glad that you both found a working solution.

I just wanted to add that if you have feedback about the error messages or documentation in this provider then you could record that by opening an issue in the hashicorp/google provider’s GitHub repository.

1 Like

The “Use provider” section shows how to minimally define the provider in HCL.

The link I provided should take you lower down in the documentation to a heading Using Terraform Cloud which is more explicit in what you need to do when configuring this provider in HCP.

Glad we got through :slight_smile:

Enjoy Terraform