terraform Verified current stable Not installed? Cloud Infrastructure

Terraform / Focus On Specific Resources

Focus On Specific Resources

Focuses Terraform operations solely on specified resources, bypassing others.

$
Terminal
terraform plan -target <resource_type.resource_name[instance_index]>

When To Use

When needing to target and apply changes to specific infrastructure resources without affecting others.

Pro Tip

Use this alongside 'plan' with '-destroy' to assess resource destruction safely before execution.

Anatomy of Output

Understanding the result

Targeting aws_instance.my_instance[0]... Target Resource

Indicates which resource will be focused on for planning.

0 to change, 0 to destroy, 1 to add Execution Summary

Details the actions to perform on the targeted resource.

Plan: 1 resource to add. Planning Result

Indicates that the specific resource will be created.

Power User Variants

Optimized versions

terraform plan -target aws_instance.my_instance[0] -var 'environment=prod'

Plan for a specific resource while passing additional environment variables.

terraform plan -target aws_instance.my_instance[0] -destroy

Plan to delete the targeted resource without affecting others.

Troubleshooting

Common pitfalls

Error: Target resource not found: aws_instance.my_instance[0]

Solution: Verify that the specified resource exists in the current Terraform state.

Error: Invalid resource targeting: multiple matches found

Solution: Refine the targeting string to specify a single resource accurately.

Error: No changes detected in target resource

Solution: Ensure that updates are made to the resource configuration if changes are expected.

Command Breakdown

What each part is doing

terraform
Base Command
The executable that performs this operation. Here it runs Terraform before the shell applies any redirect operators.
<resource_type.resource_name[instance_index]>
resource type.resource name[instance index
The value supplied for resource type.resource name[instance index.
-target
Command Option
Tool-specific option used by this command invocation.

Alternative Approaches

Comparable commands in other tools

Alternative cloud infrastructure tools for the same job.