You run a local-exec provisioner in a null resource called null_resource.run_script and realize that you need to rerun the script. Which of the following commands would you use first?
A.
terraform taint null_resource.run_script
B.
terraform apply -target=null_resource.run_script
C.
terraform validate null_resource.run_script
D.
terraform plan -target=null_resource.run_script
As discussed, the `taint` command used to be the right choice; however, it is deprecated. The right answer is:
terraform apply -replace="null_resource.run_script"
Reference:
https://developer.hashicorp.com/terraform/cli/commands/taint
You are all correct that taint has been deprecated and replaced with -replace. But neither D nor any other option here uses the -replace command. Therefore option A is the only valid option given these choices.
Not so sure, read Arrash his comment below. If a provisioner fails, it would be marked as tained by default without any user interaction required. This only leaves the apply as a required step....
The correct answer is A. terraform taint null_resource.run_script. This command marks the null resource as tainted, which means that Terraform considers the resource to be out-of-date and will recreate it during the next terraform apply run. When Terraform recreates the null resource, it will also rerun the local-exec provisioner.
Option B, terraform apply -target=null_resource.run_script, would work, but it is overkill because it would apply all the resources in the configuration, not just the null resource with the local-exec provisioner.
Option C, terraform validate null_resource.run_script, only checks the syntax of the configuration, and does not affect the state of the resource.
Option D, terraform plan -target=null_resource.run_script, generates a plan for applying changes to the configuration, but does not apply those changes, so it would not rerun the local-exec provisioner.
1) Create main.tf:
terraform {
required_version = ">= 0.13"
}
resource "null_resource" "run_script" {
provisioner "local-exec" {
command = "echo 'Hello, Terraform!' > example.txt"
}
}
2) terraform init
3) terraform apply
4) Modify the content of the example.txt file manually or delete it.
5) terraform taint null_resource.run_script
6) terraform apply
The local-exec provisioner should now run again, and the example.txt file will be recreated with the content specified in the command of the local-exec provisioner block.
As described here:
https://developer.hashicorp.com/terraform/cli/commands/taint
This command is deprecated. For Terraform v0.15.2 and later, we recommend using the -replace option with terraform apply instead
But there is no option that uses the -replace command, so option A as described in my step 5 is the only valid option.
Warning: This command is deprecated. For Terraform v0.15.2 and later, we recommend using the -replace option with terraform apply instead, So { $ terraform apply -replace="aws_instance.example[0]" } but there is no like this apply -replace option in the mentioned options so answer is A : terraform taint ..........
B. terraform apply -target=null_resource.run_script
Running the terraform apply -target=null_resource.run_script command will specifically target the null_resource.run_script resource and execute its provisioner again. This is useful when you want to rerun the local-exec provisioner without affecting other resources.
To rerun the script defined in the local-exec provisioner of a null resource called null_resource.run_script, you can use the terraform apply command with the -target flag to specifically target the null resource.
This command instructs Terraform to only apply changes to the specified target resource, which in this case is the null_resource.run_script. It will re-run the local-exec provisioner associated with that null resource.
I think point to note is "you realize that you need to rerun the script". It is NOT talking about failure. So marking it taint is the only right option, so that on next run, it can be deleted and recreated.
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.
alirasouli
Highly Voted 2 years agoSproket
Highly Voted 2 years, 4 months agoPietjeplukgeluk
1 year, 9 months agoNunyabiznes
Most Recent 1 month, 4 weeks agoBere
1 month, 4 weeks agoreynaldiekoz
3 months agohrajkuma
4 months, 1 week ago090200f
4 months, 2 weeks agoenklau
4 months, 2 weeks agogofavad926
1 year, 1 month agoledjo
1 year, 2 months agoBaburTurk
1 year, 3 months agoJayanth
1 year, 3 months agoBusi57
1 year, 4 months agonebulabc
1 year, 4 months agonebulabc
1 year, 4 months agoNi33
1 year, 6 months agoFarziWaliMarzi
1 year, 7 months agoaanataliya
1 year, 2 months agoPower123
1 year, 7 months ago