terraform, gcp,

Terraform and GCP googleapi: Error 400: Request contains an invalid argument

Feb 14, 2023 · 2 mins read · Post a comment

It would be more or less fair to say that GCP is the only Cloud hosting platform that I have disdain for and give me an urge to punch a wall. Based take, right?! While I was trying to deploy a staging database environment, I encountered the following error message: googleapi: Error 400: Request contains an invalid argument., badRequest. As for the solution, it’s not an all-round one, but here are my two cents.

Prerequisites

  • Terraform
  • GCP

Solution(s)

enable Terraform debugging

To enable the TF debug mode you need to assign value to the TF_LOG env variable. Possible values are: TRACE, DEBUG, INFO, WARN and ERROR.

Step 1. In this case, we need to set the TF_LOG variable to DEBUG, so run the following command:

export TF_LOG="DEBUG"

Step 2. Verify.

echo $TF_LOG

Step 3. Next time you run terraform plan or terraform apply the DEBUG messages will be sent to the standard output in a JSON format. But, according to the official docs, JSON encoding is not stable at the time being. However, I won’t stress things too much.

Step 4. Investigate the error logs which leads us to the second tip.

check the resource code block argument values and any used TF module (if any)

I would start with double-checking any required arguments and their respective values. Try disabling any optional arguments to make sure you are not sending any “wrong” type of value.

Regarding my GCP struggles, I was assigning a resource name to a required argument that was supposed to receive a resource ID instead.

outputs.tf

Use the outputs.ts file to your advantage in order to debug assigned values.

Step 1. Example setting the vpc_id attribute value from a network module, so we could print it in the command line output later.

output "debug_network_vpc_id" {
  description = "Debug networking module VPC_ID value."
  value       = module.network.vpc_id
}

Step 2. Find out the attribute value by running: terraform plan.

Furthermore, if you want to save the value as an output in the TF state, run terraform apply. Next, to print the value in the command line output, run: terraform output debug_network_vpc_id.

Conclusion

If you have any other great solutions on this topic, or even issues, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.