Elevating IaC Workflows with Spacelift Stacks and Dependencies šŸ› ļø

Register for the July 23 demo ā†’

Terraform

Terraform Target Flag: How to Target Resources in Terraform

How to target resources in Terraform

In this short 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!

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 a target in Terraform?

The -target option is a command flag used in Terraform to target a specific resource for action instead of performing the action on all resources. It is used when running the terraform plan, terraform apply or terraform destroy commands.

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

How do you target resources in Terraform?

Usually, your Terraform configuration file defines multiple resources. For example, this might include 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 usefully when run with theĀ planĀ command will limit the output to show the planned changes to the targeted resources only.

Example 1: Terraform apply ā€” single resource using -target=resource

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

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 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.

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.

Targeting resources with terraform plan

You can also use theĀ -targetĀ option with theĀ terraform planĀ command in the same way asĀ terraform apply.

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?

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 to avoid using target?

Targeting resources can be useful in certain situations, but it should be used with caution as it can create resource dependencies and cause unexpected changes to your infrastructure. Terraform has built-in dependency mapping that should be taken advantage of wherever possible.

If your Terraform configuration uses remote state storage (Terraform Cloud, AWS S3, etc.), the targeted approach might not be appropriate. Terraform needs to fetch the entire state to calculate changes, which nullifies the benefits of targeting specific resources.

It is also not a good idea to target resources that generate random values in isolation.

For example, consider a storage account is generated with a name using the random_petĀ resource (an interesting resource that generates random pet names that are 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 will still contain the old name for the storage account. Again this is where the built-in dependency mapping that Terraform provides should be utilized.

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.

We encourage you also to exploreĀ how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine,Ā SpaceliftĀ is a fantastic tool for this.

Note: New versions of Terraform will be 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 will expand on Terraformā€™s existing concepts and offerings. It is a viable alternative to HashiCorpā€™s Terraform, being forked from Terraform version 1.5.6. OpenTofu retained all the features and functionalities that had made Terraform popular among developers while also introducing improvements and enhancements. OpenTofu works with your existing Terraform state file, so you wonā€™t have any issues when you are migrating to it.

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

How can Spacelift stacks & dependencies elevate your IaC workflows?

Don’t miss our July 23 webinar.

Register for the webinar