Unable to use shell functions in TF_CLI_ARGS_apply

I have created a vars.sh file as below:

function func_test {

export TF_VAR_test=func_test
export TF_CLI_ARGS_apply="-auto-approve && $TF_VAR_test"

Want to run the test function only for terraform apply. But its not executing the function.
When I am trying manually terraform apply -auto-approve && $TF_VAR_test. It’s working fine.

Is this a bug? Is there a workaround?


Hi @user47,

It seems like what you were hoping to achieve here is for Terraform to execute that func_test shell function whenever you run terraform apply. Unfortunately that’s not possible, because the order of operations is the opposite of what you seem to be expecting: the shell is running Terraform, not the other way around.

Because Terraform itself isn’t a shell, it interprets the TF_CLI_ARGS_apply variable literally, and so it behaves as if you had written something like this:

terraform apply -auto-approve '&&' func_test

I’m using the ' quotes here to signify that the && is just taken literally, not interpreted as an operator by the shell. Terraform will therefore think you intend to apply a plan file called &&, which is not what you intended.

I think the closest you could get to what you attempted here would be to define a shell alias or function, separate from the terraform command itself, which runs the sequence of commands you want to run. For example:

alias terraform_test='terraform apply -auto-approve && some-cmd'

You can then run terraform_test instead of terraform apply to get the effect you wanted.

Thanks, it’s working.