Skip to main content
In this lesson, you’ll learn how to use resource targeting with OpenTofu’s plan and apply commands to update individual resources without affecting the rest of your infrastructure.

Example Configuration

Below is a sample HCL configuration that generates a random string and launches an AWS EC2 instance tagged with that string:
The aws_instance.web resource uses the interpolation ${random_string.server-suffix.id} to append the generated suffix to the Name tag.

Applying Changes Normally

If you change the random string’s length from 6 to 5 and run:
OpenTofu will plan and apply updates for both resources because the EC2 tag depends on the random string:

Targeting a Single Resource

To update only the random string and leave the EC2 instance untouched, use the -target flag:
OpenTofu will generate a plan for just that resource:
Using resource targeting can leave your state incomplete. Reserve this feature for urgent fixes or small, isolated changes.

CLI Commands Reference

Watch Video