How to fix Invalid function argument on line in provider credentials file google Invalid value for path parameter no file exists
Working with terraform can tricky sometime and specially if you are working with cloud service provider such as Google Cloud Platform.
I have faced the following issue while I was working with my first Terrafrom project with Google Cloud.
Error Invalid function argument on main.tf line 2, in provider google credentials = file(gcp-account.json) Invalid value for path parameter no file exists at gcp-account.json this function works only with files that are distributed as part of the configuration source code, so if this file will be created by a resource in this configuration you must instead obtain this result from an attribute of that resource.
Initially the issue looked quite scary but when I looked the issue carefully then I realise - I had incorrect path for my credential JSON file.
Here is my terraform file in which I had mentioned incorrect path for gcp-account.json
1provider "google" {
2 credentials = file("/rwagh/gcp-account.json") /*Incorrect path - It should be absolute path*/
3 project = "gcp-terraform-307119"
4 region = "europe-west4"
5 zone = "europe-west4-a"
6}
7
8resource "google_compute_instance" "default" {
9 name = "test"
10 machine_type = "e2-micro"
11
12 boot_disk {
13 initialize_params {
14 image = "debian-cloud/debian-9"
15 }
16 }
17
18 network_interface {
19 network = "default"
20
21 access_config {
22 // Ephemeral IP
23 }
24 }
25}
How to fix?
To fix this issue you should always use absolute path inside your terraform file.
Example -
1credentials = file("<Absolute path to .pem file e.g /home/rwagh/id_rsa.pem>")
2
3//or
4
5credentials = file("<Absolute path to .json file e.g /home/rwagh/gcp-account.json>")
Here is my final terraform file after replacing with absolute path
1provider "google" {
2 credentials = file("/home/rwagh/gcp-account.json") /*Use absolute path*/
3 project = "gcp-terraform-307119"
4 region = "europe-west4"
5 zone = "europe-west4-a"
6}
7
8resource "google_compute_instance" "default" {
9 name = "test"
10 machine_type = "e2-micro"
11
12 boot_disk {
13 initialize_params {
14 image = "debian-cloud/debian-9"
15 }
16 }
17
18 network_interface {
19 network = "default"
20
21 access_config {
22 // Ephemeral IP
23 }
24 }
25}
Read More -
- Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
- How to setup Virtual machine on Google Cloud Platform using terraform
- Create EC2 Instance on AWS using terraform
- How to use Terraform Input Variables
- What is variable.tf and terraform.tfvars?
- How to use Terraform locals?
- How to use Terraform output values?
- Understanding terraform count, for_each and for loop?
- Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
- How to use Terraform Dynamic blocks?
- How to use Terraform resource meta arguments?
- How to use Terraform Data sources?
- What is terraform provisioner?
- Terraform how to do SSH in AWS EC2 instance?
- How Terraform modules works?
- How to run specific terraform resource?
- How to use Workspaces in Terraform?
- Securing AWS secrets using HashiCorp Vault with Terraform?
- Managing Terraform states?
- Terraform state locking using DynamoDB (aws_dynamodb_table)
- Terraform and AWS credentials handling?
- Terraform and AWS Multi account Setup?
Terragrunt -
Posts in this Series
- How to use Terragrunt?
- Terraform and AWS Multi account Setup?
- Terraform and AWS credentials handling?
- How to fix-error configuring S3 Backend no valid credential sources for S3 Backend found?
- Terraform state locking using DynamoDB (aws_dynamodb_table)?
- Managing Terraform states?
- Securing AWS secrets using HashiCorp Vault with Terraform?
- How to use Workspaces in Terraform?
- How to run specific terraform resource, module, target?
- How Terraform modules works?
- Terraform how to do SSH in AWS EC2 instance?
- What is terraform provisioner?
- Is terraform destroy needed before terraform apply?
- How to fix terraform error Your query returned no results. Please change your search criteria and try again?
- How to use Terraform Data sources?
- How to use Terraform resource meta arguments?
- How to use Terraform Dynamic blocks?
- Terraform - How to nuke AWS resources and save additional AWS infrastructure cost?
- Understanding terraform count, for_each and for loop?
- How to use Terraform output values?
- How to use Terraform locals?
- How to fix error configuring Terraform AWS Provider error validating provider credentials error calling sts GetCallerIdentity SignatureDoesNotMatch?
- How to fix Invalid function argument on line in provider credentials file google Invalid value for path parameter no file exists
- How to fix error value for undeclared variable a variable named was assigned on the command line?
- What is variable.tf and terraform.tfvars?
- How to use Terraform Input Variables
- Terraform create EC2 Instance on AWS
- How to fix Error creating service account googleapi Error 403 Identity and Access Management (IAM) API has not been used in project before or it is disabled
- How to setup Virtual machine on Google Cloud Platform
- Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11