How to Use Terraform Untaint: Syntax and Examples
Use the
terraform untaint command followed by the resource address to mark a resource as healthy, so Terraform will not recreate it on the next apply. This command reverses the effect of terraform taint and helps fix accidental taints.Syntax
The terraform untaint command requires the address of the resource you want to mark as healthy. The general syntax is:
terraform untaint [options] RESOURCE_ADDRESS
Here, RESOURCE_ADDRESS is the full name of the resource in your Terraform configuration, like aws_instance.example.
bash
terraform untaint aws_instance.example
Example
This example shows how to untaint an AWS EC2 instance resource named aws_instance.web_server. Suppose you previously marked it as tainted to force recreation, but now you want to keep the existing instance.
bash
terraform untaint aws_instance.web_server
Output
Resource instance aws_instance.web_server has been marked as not tainted.
Common Pitfalls
Common mistakes when using terraform untaint include:
- Using the wrong resource address, which causes an error because Terraform cannot find the resource.
- Trying to untaint a resource that is not tainted, which results in a message that no action is needed.
- Forgetting to run
terraform applyafter untainting, so the state is not updated in the infrastructure.
Always verify the resource address with terraform state list before untainting.
bash
Wrong: terraform untaint aws_instance.wrong_name Right: terraform untaint aws_instance.correct_name
Quick Reference
| Command | Description |
|---|---|
| terraform taint RESOURCE_ADDRESS | Marks a resource as needing recreation |
| terraform untaint RESOURCE_ADDRESS | Marks a resource as healthy, cancels taint |
| terraform state list | Lists all resources and their addresses |
| terraform apply | Applies changes to infrastructure |
Key Takeaways
Use
terraform untaint RESOURCE_ADDRESS to mark a tainted resource as healthy.Verify the exact resource address with
terraform state list before untainting.Untainting does not change infrastructure until you run
terraform apply.Trying to untaint a non-tainted resource will have no effect.
Always double-check resource names to avoid errors.