Terraform-provider-google: Google Credentials not working using environment variables

Created on 8 Nov 2017  ·  5Comments  ·  Source: hashicorp/terraform-provider-google

_This issue was originally opened by @naoleyrashmi as hashicorp/terraform#16587. It was migrated here as a result of the provider split. The original body of the issue is below._


Hi there,
Google Credentials not working using environment variables . As i need to build the whole infrastructure using terraform for GCP . I need to pass the value of credentials via an environment variable which is stored at one location.

Variables Tried:

GOOGLE_CREDENTIALS
GOOGLE_CLOUD_KEYFILE_JSON
GCLOUD_KEYFILE_JSON

Terraform Version

Terraform v0.10.8

Terraform Configuration Files

// Configure the Google Cloud provider
provider "google" {
project = "${var.gce_project}"
region = "${var.gce_region}"
credentials = "${var.GCLOUD_KEYFILE_JSON}"
}

resource "google_compute_instance" "testvm" {
name = "test"
machine_type = "f1-micro"
zone = "us-east1-c"

tags = ["test1", "vm"]

boot_disk {
initialize_params {
image = "debian-cloud/debian-8"
}
}

network_interface {
subnetwork = "default"

access_config {
  // Ephemeral IP
}

}
}

Error Output

provider.google: credentials are not valid JSON '/home/rashmin/GCTF/01-key/credentials.json': invalid character '/' looking for beginning of value

Expected Behavior

terraform plan should work

Actual Behavior

Got below error when running terraform plan
Error: provider.google: credentials are not valid JSON '/home/rashmin/GCTF/01-key/credentials.json': invalid character '/' looking for beginning of value

Steps to Reproduce

Set the environment variables :
export TF_VAR_GCLOUD_KEYFILE_JSON=/home/rashmin/GCTF/01-key/credentials.json
export TF_VAR_gce_project=terraformgcp
export TF_VAR_gce_region=us-central

  1. terraform init
  2. terraform plan
  3. terraform apply

Important Factoids

Issue is not faced when the variable is not passed for credentials. If the explicit path is provided terraform works seamlessly fine.

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:
none

question

Most helpful comment

Hey @naoleyrashmi, glad you were able to get it working!

For future reference: the environment variable requires the full file contents, not just the path. So you would say something like:
export TF_VAR_GCLOUD_KEYFILE_JSON=$(cat /home/rashmin/GCTF/01-key/credentials.json )
or inside your provider you could say:
credentials = "${file("${var.GCLOUD_KEYFILE_JSON}")}"

All 5 comments

Hello Team,

I am no longer facing this issue. Using GOOGLE_APPLICATION_CREDENTIALS resolved the issue . Thanks a lot.

Hey @naoleyrashmi, glad you were able to get it working!

For future reference: the environment variable requires the full file contents, not just the path. So you would say something like:
export TF_VAR_GCLOUD_KEYFILE_JSON=$(cat /home/rashmin/GCTF/01-key/credentials.json )
or inside your provider you could say:
credentials = "${file("${var.GCLOUD_KEYFILE_JSON}")}"

Thanks Dana :)

please some one explain in details with GCP workspace terraform

Thanks in advance

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

Was this page helpful?
0 / 5 - 0 ratings