How to pass multiple vars to terraform command in gradle exec task
As if it was easy!
I can't believe I wasn't able to find a single example on the internet about how to use the terraform executable with Java ProcessBuilder, Runtime.exec, Gradle's Exec task or anything else. How hard can it be might be your question. The problem is that it's not to intuitive and easy how to pass args when not just directly typing everything by hand on the shell directly.
In my case, I needed to pass mutliple var options into a terraform plan command. On the command line, this may look like
terraform plan -var foo=bar -var bar=baz -var-file=variables.prod.tfvars
Or as described in the official documentation, it could be
terraform apply -var="image_id=ami-abc123"or
terraform apply -var='image_id_list=["ami-abc123","ami-def456"]' -var="instance_type=t2.micro"
There is even more documentation in order to make this thing work across different operating systems.
What struck me was apparently the whitespace between -var and the key value pair itself. Took me round about an hour to figure out this is the correct way to feed a command into a Gradle Exec task (Kotlin DSL ahead):
Mind that every var requires you to pass in an arg of -var and an arg of the key value pair itself.