David
David

Reputation: 28

Weirdness Executing Terraform from Powershell

So I have a little project going and the output is a bunch of terraform.tf files and some PowerShell scripts to deploy those resources defined in the .tf files.

Within one of my PowerShell files I dynamically create the options to pass to the terraform executable. This is where it gets weird.

So the endstate command that I want to run is this:

terraform plan -out=CustomerName-Plan -var-file ..//config-dev.tfvars -var-file ..//config-prd.tfvars -var-file ..//config-uat.tfvars -var-file ..//vars-priv.tfvars

What I have in my PowerShell script is this:

terraform plan $planOption $options

Further up in the PowerShell script is some code that makes $planOption = "-out=CustomerName-Plan" and $options="-var-file ..//config-dev.tfvars -var-file ..//config-prd.tfvars -var-file ..//config-uat.tfvars -var-file ..//vars-priv.tfvars"

At the moment if I run the terraform plan ... all fully qualified and no variables from a PowerShell commandline it all works fine.

If I run the script with terraform plan $planOption (and hardcoded -varafile options) it works fine.

If I run the script with terraform plan $planOption $options I get a vague Terraform error that explains the usage of Terraform plan like so:

Usage: terraform plan [options] [DIR]

Generates an execution plan for Terraform.

This execution plan can be reviewed prior to running apply to get a sense for what Terraform will do. Optionally, the plan can be saved to a Terraform plan file, and apply can take this plan file to execute
this plan exactly.

Options:

-compact-warnings If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.

-destroy ....etc

So What I have surmised is that I have a syntax problem somewhere in the $options variable but I cannot see where it is or what the problem is. I've also tried concatenating all the $options and $planOptions variables into another variable and passing that to terraform with the same error result. I've also tried calling terraform with an "Invoke-Command -scriptblock bit of code but that didn't work either. Oh and I've tried all sorts of variations of "-var-file ../path" & "-var-file=../path" & "-var-file ..//path" and then backslashes as well.

Any hints or tips that anyone can give me to work out what I'm doing not quite right would be awesome!

In the meantime if I work it out I'll post back here, as this has been driving me nuts for two days now.

Kind regards,

Dave.

Upvotes: 0

Views: 658

Answers (1)

David
David

Reputation: 28

OK So I've managed to find an acceptable workaround.

Invoke-Expression -Command $TerraCMD

So TerraCMD is a concatenation of the plan options and all the var file options and the actual terraform command and it all works perfectly.

The contents of $TerraCMD is: terraform plan -out=CustomerName-Plan -var-file ..//config-dev.tfvars -var-file ..//config-prd.tfvars -var-file ..//config-uat.tfvars -var-file ..//vars-priv.tfvars

All is now good in my world. Thanks to anyone that even looked at this post. :)

Upvotes: 0

Related Questions