Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Apply A Single Terraform Module?

Tags:

terraform

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?

like image 786
opticyclic Avatar asked Jan 22 '26 01:01

opticyclic


1 Answers

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.

like image 72
Matt Schuchard Avatar answered Jan 27 '26 01:01

Matt Schuchard