Learn to use resource targeting with OpenTofu’s commands to update individual resources without affecting the rest of your infrastructure.
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.
To update only the random string and leave the EC2 instance untouched, use the -target flag:
Copy
Ask AI
$ tofu apply -target=random_string.server-suffix
OpenTofu will generate a plan for just that resource:
Copy
Ask AI
OpenTofu will perform the following actions: # random_string.server-suffix must be replaced-/+ resource "random_string" "server-suffix" { ~ id = "bl12qd" -> (known after apply) ~ length = 6 -> 5 # forces replacement}Plan: 1 to add, 0 to change, 1 to destroy.Warning: Resource targeting is in effectEnter a value: yesrandom_string.server-suffix: Destroying... [id=6r923x]random_string.server-suffix: Creation complete after 0s [id=nglmpo]Warning: Applied changes may be incompleteApply complete! Resources: 1 added, 0 changed, 1 destroyed.
Using resource targeting can leave your state incomplete. Reserve this feature for urgent fixes or small, isolated changes.