Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run terraform init from a different folder?

I want to script terraform for CI/CD purpose and I don't like CDing in scripts, I rather have specific paths.

I tried terraform init c:\my\folder\containing\tf-file

But running that puts the .terraform folder in my cwd.

like image 709
red888 Avatar asked Nov 13 '17 21:11

red888


1 Answers

I know this is an old thread but... The command you are looking for is:

terraform -chdir=environments/production apply

Please see this link for help with the global option -chdir=":

Quote from the actual Terraform site:

The usual way to run Terraform is to first switch to the directory containing the .tf files for your root module (for example, using the cd command), so that Terraform will find those files automatically without any extra arguments.

In some cases though — particularly when wrapping Terraform in automation scripts — it can be convenient to run Terraform from a different directory than the root module directory. To allow that, Terraform supports a global option -chdir=... which you can include before the name of the subcommand you intend to run:

terraform -chdir=environments/production apply The chdir option instructs Terraform to change its working directory to the given directory before running the given subcommand. This means that any files that Terraform would normally read or write in the current working directory will be read or written in the given directory instead.

like image 98
Don-Pierre Halfaway Avatar answered Oct 06 '22 11:10

Don-Pierre Halfaway