opticyclic
opticyclic

Reputation: 8146

How Do I Apply A Single Terraform Module?

I have a single main.tf at the root and different modules under it for different parts of my Azure cloud e.g.

main.tf
  - apim
  - firewall
  - ftp
  - function

The main.tf passes variable down to the various modules e.g. resource group name or a map of tags.

During development I have been investigating certain functionality using the portal and I don't have it in terraform yet.

e.g. working out how best to add a mock service in the web module

If I now want to update a different module (e.g. update firewall rules) terraform will suggest destroying the added service.

How can I do terraform plan/apply for just a single module?

Upvotes: 12

Views: 22303

Answers (2)

JAA
JAA

Reputation: 107

Per Matthew's answer, I found that terraform didn't like -target=module.<declared module name>

My terraform plan / apply works with:

terraform plan -target module.<declared module name>

In addition, it's also sometimes helpful to use -target to destroy a module (again, only if it arises during development / testing).

For example:

terraform destroy -target module.the_firewall

This is using terraform version: 1.3.9

Upvotes: 5

Matthew Schuchard
Matthew Schuchard

Reputation: 28864

You can target only the module by specifying the module namespace as the target argument in your plan and apply commands:

terraform plan -target=module.<declared module name>

For example, if your module declaration was:

module "the_firewall" {
  source = "${path.root}/firewall"
}

then the command would be:

terraform plan -target=module.the_firewall

to only target that module declaration.

Note that this should only be used in development/testing scenarios, which it seems you already are focused on according to the question.

Upvotes: 26

Related Questions