33

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.

RaGe
  • 22,696
  • 11
  • 72
  • 104
red888
  • 27,709
  • 55
  • 204
  • 392
  • 1
    In the next release there will be a `TF_DATA_DIR` env variable that can be set, but this isn't necessarily it's intended purpose, as you will want to make sure it's set differently for each config you want to target. – JimB Nov 14 '17 at 18:56

5 Answers5

27

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.

7

Specify directory without option

terraform apply [options] ./path-to-dir

Or you could use -chdir option

terraform -chdir="./path-to-dir" apply
Kole Kole
  • 189
  • 6
  • 12
  • 1
    The problem with -chdir is it will change the relative path for all parameters. It doesn't help for cases where the .tf files and .tfvars files are in different directories – John Heyer Jul 28 '21 at 03:12
4

The [DIR] option in terraform init command tells terraform where to process tf files from, but doesn't tell it where to store state files. If you want to change what local path state files are stored at, add a section to your main.tf to configure backend path.

terraform {
  backend "local" {
    path = "relative/path/to/terraform.tfstate"
  }
}

I haven't actually tried this out, but you may be able to pass the path from command line as:

-backend-config="path=/foo"
RaGe
  • 22,696
  • 11
  • 72
  • 104
4

Terraform by default assumes that you are running your command in directory where terraform files are there but in case you are different directory and want to run terraform commands on files which are located elsewhere, you can do following:

terraform -chdir=[path_to_dir] [command_to_run]

Example:

terraform -chdir=terraform/ plan
terraform -chdir=terraform/ apply
Sukhmeet Sethi
  • 616
  • 5
  • 14
2

UPDATE: Terraform cli now supports -chdir=PATH/TO/TF_files

https://www.terraform.io/cli/commands#switching-working-directory-with-chdir

By default, terraform init assumes that the working directory already contains a configuration and will attempt to initialize that configuration.

Optionally, init can be run against an empty directory with the -from-module=MODULE-SOURCE option, in which case the given module will be copied into the target directory before any other initialization steps are run.

https://www.terraform.io/docs/commands/init.html

strongjz
  • 4,271
  • 1
  • 17
  • 27
  • 4
    I don't think you understand my question? I have a folder with my tf file in it. I don't want to have to CD to that dir in order to run init and other terraform commands, i want to run terraform from anywhere and just explicitly specify the folder. According to the docs I can just specify a folder as a arg but when I do that it still creates .terraform in the cwd. using -from-module=mydir it actually copies the tf from mydir dir to my cwd – red888 Nov 14 '17 at 15:13
  • 2
    @red888: terraform is dependent on the current working directory. There's no way around that. – JimB Nov 14 '17 at 18:42
  • 4
    looks like this is a new feature they should add. i would certainly love to be able to do so. – Prachi Oct 28 '18 at 01:21