šŸŽ‰Spacelift raises $51M Series C to redefine the future of infrastructure

āž”ļø Learn more

Terraform

Terraform Target Flag: How to Target Resources in Terraform

How to target resources in Terraform

šŸš€ Level Up Your Infrastructure Skills

You focus on building. We’ll keep you updated. Get curated infrastructure insights that help you make smarter decisions.

In this article, we will look at how to target specific resources in Terraform. We will explain the use of the -targetĀ option, and how to use it to target single and multiple resources with some examples, and in what circumstances you should avoid using it!

What we will cover:

  1. What is a target in Terraform?
  2. How do you target resources in Terraform?
  3. How to target Terraform module?
  4. Targeting resources with terraform plan
  5. How do you target destroy in Terraform?
  6. When to avoid using Terraform -target?

What is target in Terraform?

In Terraform, -target is a command-line option that lets you apply or plan changes to specific resources only, rather than performing the action on all resources. It can be useful for isolating updates, debugging, or speeding up workflows in large deployments.Ā 

Without the -target option, the terraform plan, terraform apply or terraform destroy commands would take the entire configuration into account rather than a specific resource or set of Terraform resources.

However, since the -target flag causes Terraform to focus only on the targeted resource and its dependencies, skipping unrelated parts of the graph. This can lead to partial updates and potentially skip changes that Terraform would normally apply, which risks drift or inconsistent state. It’s mainly used for debugging or urgent fixes, not routine workflow.

How do you target resources in Terraform?

Your Terraform configuration file usually defines multiple resources, such as an Azure Virtual Machine and an Azure Storage account.

You can use the -target option to specify a resource identifier. With the apply and destroy commands, the operation will then apply to the target resources only, and when run with the plan command, the output will be limited to show the planned changes to the targeted resources only.

Example 1: Terraform apply — single resource using -target=resource

To apply changes to a single resource in Terraform, use the -target option with the full resource address during terraform apply.

In our example, to apply changes only to the Azure Virtual Machine and ignore all other resources, you can run terraform apply command with the -target option to reference the VM:

terraform apply -target=azurerm_vm.example

In this command, azurerm_vm.example is the resource identifier of the Virtual Machine instance resource defined in your Terraform configuration file.

Example 2: Terraform apply — multiple resources using -target=resource

To target multiple resources in Terraform, use multiple -target flags in your command. Each -target must specify a full resource address. This selectively applies or plans only the listed resources and their dependencies.Ā 

For example, to apply changes to your Azure Virtual Machine and Azure Storage Account only and ignore all other resources, you can run terraform apply command with the -target option to reference both resources:

terraform apply -target=azurerm_vm.example -target=azurerm_storage.example

In this command, azurerm_vm.example is the resource identifier of the Virtual Machine instance resource, and azurerm_storage.example is the resource identifier of the Azure Storage account defined in your Terraform configuration file.

Note: Overusing targeting can cause drift or incomplete state changes, so it’s best used for troubleshooting or partial updates.

How to target Terraform module?

To target a Terraform module, you can use theĀ -targetĀ option followed by the module path and the resource identifier. The format isĀ <module_path>.<resource_identifier>.

Terraform will also apply changes to all resources that depend on that module, so be sure to understand the dependencies in your Terraform configuration before targeting a module.

To apply changes to your Azure Virtual Machine when defined in a module only and ignore all other resources, you can runĀ terraform applyĀ command with theĀ -targetĀ option to reference the VM only:

terraform apply -target=module.vm.azurerm_vm.example

In this command, the module is calledĀ vm, where the resource identifier isĀ azurerm_vm.example.

How to target multiple Terraform modules

To target multiple Terraform modules, use the -target flag with each module path explicitly defined during terraform apply or terraform plan.

Each module must be specified with its full resource or module address, for example:

terraform apply -target=module.network -target=module.database

To manage multiple modules more safely, structure your project with clear module boundaries and use workspaces or separate state files when isolation is required.

Targeting resources with terraform plan

You can also use the -target option with the terraform plan command in the same way as terraform apply, using the -target flag followed by the resource address. This limits the plan to only the specified resources and their dependencies.

For example, to reference a VM only:

terraform plan -target=azurerm_vm.example

Read more about targeting resources with terraform plan.

How do you target destroy in Terraform?

To target and destroy a specific resource in Terraform, use the -target flag with the terraform destroy command. This destroys only the specified resource without affecting the rest of the infrastructure.Ā 

Use this feature carefully, as destroying a single resource may break dependencies or state consistency if other resources depend on it. Always run terraform plan first to verify the impact.

For example, similar to the apply andĀ plan command, to destroy your Azure Virtual Machine only and ignore all other resources, you can runĀ terraform destroy command with theĀ -target flag to reference the VM only:

terraform destroy -target=azurerm_vm.example

When should you avoid using the target flag in Terraform?

Avoid using the -target flag in Terraform during standard or routine deployments, as it can lead to inconsistent infrastructure states and break Terraform’s dependency graph.

The -target flag forces Terraform to focus only on specific resources, bypassing the usual dependency-aware planning and apply logic. This is useful for debugging or deploying isolated changes, but overuse can create drift, partial updates, or unintended side effects.

Reserve -target for exceptional cases like:

  • Isolating a failing resource to unblock a deployment
  • Manually reapplying a resource after an external change
  • Migrating or replacing a specific module without affecting others

For example, consider a storage account generated with a name using the random_pet resource (an interesting resource that generates random pet names intended to be used as unique identifiers for other resources).Ā 

Targeting this in isolation would be untrustworthy if we decide to create a new storage account using random_pet as the value in the state file, as it will still contain the old storage account name. Again, this is where Terraform’s built-in dependency mapping should be utilized.

Managing Terraform resources with Spacelift

Terraform is really powerful, but to achieve an end-to-end secure Gitops approach, you need to use a product that can run your Terraform workflows.Ā SpaceliftĀ takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:

  • Policies (based on Open Policy Agent) – You can control how many approvals you need for runs, what kind of resources you can create, and what kind of parameters these resources can have, and you can also control the behavior when a pull request is open or merged.
  • Multi-IaC workflows – Combine Terraform with Kubernetes, Ansible, and other infrastructure-as-code (IaC) tools such as OpenTofu, Pulumi, and CloudFormation,Ā  create dependencies among them, and share outputs
  • Build self-service infrastructure – You can useĀ BlueprintsĀ to build self-service infrastructure; simply complete a form to provision infrastructure based on Terraform and other supported tools.
  • Integrations with any third-party tools – You can integrate with your favorite third-party tools and even build policies for them. For example, see how toĀ integrate security toolsĀ in your workflows using Custom Inputs.

Pomelo’s initial approach to managing Terraform was not up to the task of accommodating the needs of a rapidly expanding company. Now that they use Spacelift, they can rely on a specialized platform for infrastructure as code (IaC) that will adapt to their needs as they scale and provide a cost-effective alternative to largely manual processes.

Spacelift customer case study

Read the full story

Spacelift enables you to create private workers inside your infrastructure, which helps you execute Spacelift-related workflows on your end. Read theĀ documentationĀ for more information on configuring private workers.

You can check it out for free byĀ creating a trial accountĀ orĀ booking a demo with one of our engineers.

Key Points

TheĀ -target argument is another useful tool that can be used to select single or sets of defined resources to be sure that all other changes to all other resources in your Terraform configuration files are ignored.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Automate Terraform deployments with Spacelift

Automate your infrastructure provisioning, and build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization, and many more.

Start free trial