In this lesson, you’ll learn how to use resource targeting with OpenTofu’sDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
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 from6 to 5 and run:
Targeting a Single Resource
To update only the random string and leave the EC2 instance untouched, use the-target flag:
Using resource targeting can leave your state incomplete. Reserve this feature for urgent fixes or small, isolated changes.
CLI Commands Reference
| Command | Description |
|---|---|
tofu plan | Preview all changes before applying them |
tofu apply | Apply all planned changes |
tofu apply -target=<RESOURCE_TYPE>.<NAME> | Apply changes only to the specified resource |