Allan Xu
Allan Xu

Reputation: 9298

Is there any way to have Terraform CLI runs scripts in a directory other than PWD?

I understand that when I run terraform CLI within a directory, it takes all the terraform artifacts from the current directory.

Is it possible to have terraform apply and terraform plan to look for scripts in a directory other than current PWD such that I don't have to keep changing current directory?

Upvotes: 1

Views: 7229

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74209

At the time I'm writing this, Terraform v0.14.0 is currently in release candidate and its final release is expected in the next few weeks.

That new version will introduce a new feature to allow requesting that Terraform switch directory itself before running any of its subcommands. For example:

terraform -chdir=subdirectory init
terraform -chdir=subdirectory apply

This is essentially the same as launching Terraform with the current working directory already set to the given subdirectory, but with two minor differences:

  • Terraform will set the path.cwd value to be the directory where Terraform was run from, rather than the subdirectory. As before, path.root is the directory containing the root module, so both directories are available.
  • If your CLI configuration includes paths that Terraform resolves during its startup then they will be resolved relative to the original working directory, because the directory switch applies only to the behavior of the given subcommand, not to actions Terraform takes prior to running the subcommand.

Neither of these differences are usually significant for common Terraform use, so in most cases you can think of the -chdir= option as having the same effect as switching into the target directlry using cd first.

Upvotes: 6

Related Questions