Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform : Specifying the working directory when running terraform apply/plan

Is there any way (or workaround) to specify the working directory when running terraform apply or terraform plan ? For example :

terraform apply working_dir/vpc
terraform apply working_dir/dns
terraform apply working_dir/postgres
terraform apply working_dir/service-a
terraform apply working_dir/service-b

I know there's a target option where we can specify the resources to target. But here I need a folder to have more abstraction. The final goal is to be able to setup the infrastructure running the following commands :

make vpc
make dns
make postgres
make service-a
make service-b
like image 783
Yaya Avatar asked Feb 24 '18 01:02

Yaya


People also ask

How do I change Terraform working directory?

These days you can also use global option -chdir: The chdir option instructs Terraform to change its working directory to the given directory before running the given subcommand.

What happens when you run Terraform apply without specifying a plan file?

Without a saved plan file, terraform apply supports all planning modes and planning options available for terraform plan . Planning Modes: These include -destroy , which creates a plan to destroy all remote objects, and -refresh-only , which creates a plan to update Terraform state and root module output values.

Do you need to run Terraform plan before apply?

Running Terraform Apply in Automation In automation terraform apply can be run after the plan stage, passing in the plan output file. If there is no plan stage prior to the apply stage (not recommended), then the terraform apply -auto-approve option can be used.


2 Answers

These days you can also use global option -chdir:

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.

For example:

terraform -chdir=environments/production apply
like image 56
Marcin Avatar answered Oct 30 '22 11:10

Marcin


You can specify a plan or directory when issuing the apply command. Your examples are valid apply commands.

Usage: terraform apply [options] [dir-or-plan]

like image 44
logan rakai Avatar answered Oct 30 '22 13:10

logan rakai