Yaya
Yaya

Reputation: 268

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

Upvotes: 22

Views: 33560

Answers (3)

Marcin
Marcin

Reputation: 238199

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

Upvotes: 34

Ashish
Ashish

Reputation: 69

Execute below commands from parent directory having multiple directories containing terraform (*.tf) files.

To execute terraform:

terraform init dir-name
terraform validate dir-name
terraform plan -detailed-exitcode -out=plan-name.out dir-name
terraform apply plan-name.out

To destroy use:

terraform destroy dir-name

Upvotes: 0

logan rakai
logan rakai

Reputation: 2557

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]

Upvotes: 5

Related Questions