While deploying a virtual machine, the first launch user_data script fails due to race condition with another resource deployed during the same Terraform run.
What is the least disruptive method to correct the issue?
A.
Run terraform taint against the virtual machine’s resource name, then terraform apply
B.
Restart the virtual machine from the cloud portal
I'd go with A.
If a user_data script fails during the creation of an EC2 instance, Terraform will not mark the resource as tainted automatically. This is because Terraform does not have visibility into the success or failure of the user_data script, as it is executed within the instance itself, and not directly controlled by Terraform.
However, if the user_data script failure causes the instance to become unresponsive or otherwise unusable, Terraform may detect this during a subsequent plan or apply operation and mark the instance as "tainted". A tainted resource is one that Terraform cannot verify the current state of, and must be recreated in order to bring it back to a known state.
The script deployment will not have been tracked within Terraform - and will therefore not re-run on the next 'apply' command. The resource the script is running on will have to be redeployed to trigger the script to run a second time - the least disruptive way do achieve this is with the 'taint' command
If a creation-time provisioner fails, the resource is marked as tainted. A tainted resource will be planned for destruction and recreation upon the next terraform apply.
https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax
if it fails, you just re-run terraform apply again , why even run taint for something that didn't even got created in the first place.
For example, you're trying to create Resource A, but it fails because resource B was being created. So what do you do to create resource A again ? - terraform apply
In this scenario, the user_data script failed due to a race condition with another resource being deployed simultaneously during the same Terraform run. The least disruptive method to correct the issue is to use the terraform taint command followed by terraform apply.
Running terraform taint against the virtual machine's resource name marks the resource as tainted. Tainting a resource indicates that it has a problem and should be destroyed and recreated during the next terraform apply. By doing this, you force Terraform to recreate the virtual machine with the corrected user_data script, resolving the race condition without affecting other resources.
The question leaves room for interpretation. If a creation-time provisioner fails, the resource is marked as tainted. A tainted resource will be planned for destruction and recreation upon the next terraform apply. https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
zaaath
Highly Voted 1 year, 7 months agoMantis
Highly Voted 1 year, 6 months agolavendran93
Most Recent 2 months, 1 week agoalex78
9 months, 4 weeks agoF_M
2 months, 3 weeks agoMarch2023
1 year, 5 months agoNi33
1 year, 6 months agoOleg_gol
1 year, 6 months agoNunyabiznes
1 year, 7 months agocamps
1 year, 7 months agoPietjeplukgeluk
1 year, 9 months agoDavid_C_90
1 year, 8 months agoAbuu
1 year, 10 months agoArizonaClassics
1 year, 10 months ago