Understanding Terraform Taint and How to Use It
By Łukasz Kallas
- Published on
Sharing
In Terraform, sometimes you may want to force a particular resource to be recreated without having to delete or modify its configuration manually. That's where the terraform taint
command comes in.
What is terraform taint
?
The terraform taint command marks a resource for destruction and recreation the next time you run terraform apply. This is useful if you want to forcefully rebuild a resource due to issues, changes in external factors, or to ensure its correct state.
Key Benefits
- Force Rebuild: If a resource is behaving unexpectedly or was modified outside of Terraform, you can use taint to recreate it.
- Selective Update: Instead of applying changes to the entire infrastructure, you can taint just the resource you want recreated.
Hands-On: Using terraform taint
Step 1: Taint the resource
terraform taint aws_instance.example
This marks the aws_instance.example resource for recreation.
Step 2: Apply the Changes
terraform apply
Terraform will destroy and recreate the specified resource during this operation, leaving other resources unchanged.
Step 3: Untaint (Optional)
If you taint a resource by mistake or want to undo the action before applying, you can use the terraform untaint command:
terraform untaint aws_instance.example