Targeting Specific Resources
You'll learn how to use the -target flag to work with individual resources instead of your entire infrastructure.
Sometimes you don't want Terraform to touch your entire configuration—you just need to update, create, or destroy one specific resource. Maybe you're troubleshooting a single component, or you need to quickly fix something without waiting for a full apply cycle.
The -target Flag
The -target flag lets you tell Terraform: "Only focus on this resource." You can use it with plan, apply, and destroy commands.
The syntax looks like this:
terraform apply -target=resource_type.resource_name
For example, if you have a storage bucket defined as resource "aws_s3_bucket" "logs", you'd target it with:
terraform plan -target=aws_s3_bucket.logs
terraform apply -target=aws_s3_bucket.logs
terraform destroy -target=aws_s3_bucket.logs
When to Use Targeting
Think of targeting like using a flashlight in a dark room—you're illuminating just one thing instead of turning on all the lights. It's useful for:
- Debugging: Test changes to one resource without affecting others
- Quick fixes: Update a single broken component
- Dependency issues: Work around circular dependencies temporarily
Important Warning
Targeting breaks Terraform's normal dependency tracking. If you destroy a resource that others depend on, those dependent resources won't be automatically handled. Use targeting sparingly and cautiously—it's a power tool, not a daily driver.
Key Takeaway: The -target flag lets you scope Terraform operations to specific resources, giving you surgical control but bypassing normal dependency management—use it carefully for debugging and quick fixes.