0
0
Terraformcloud~10 mins

Terraform apply -replace flag - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform apply -replace flag
Start terraform apply
Check -replace flag?
NoNormal apply
Yes
Mark specified resource for replacement
Destroy old resource
Create new resource
Apply other changes
Finish apply
Terraform apply checks if the -replace flag is used. If yes, it forces replacement of specified resources by destroying and recreating them during apply.
Execution Sample
Terraform
terraform apply -replace=aws_instance.example
This command forces Terraform to replace the aws_instance.example resource during apply.
Process Table
StepActionResourceState BeforeState AfterNotes
1Start apply-aws_instance.example: existingaws_instance.example: existingBegin terraform apply process
2Check -replace flag----replace=aws_instance.example detected
3Mark resource for replacementaws_instance.exampleexistingmarked for replacementResource flagged to be destroyed and recreated
4Destroy old resourceaws_instance.examplemarked for replacementdestroyedOld instance is destroyed
5Create new resourceaws_instance.exampledestroyedcreatedNew instance is created
6Apply other changes---Apply any other planned changes
7Finish apply-aws_instance.example: createdaws_instance.example: createdApply completes successfully
💡 All resources applied; replaced resource destroyed and recreated as requested by -replace flag
Status Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
aws_instance.exampleexistingmarked for replacementdestroyedcreatedcreated
Key Moments - 3 Insights
Why does Terraform destroy the resource before creating a new one when using -replace?
Terraform must destroy the existing resource first to avoid conflicts and ensure a clean replacement, as shown in steps 4 and 5 of the execution_table.
Does -replace affect resources not specified in the flag?
No, only the resource specified with -replace is destroyed and recreated; other resources are applied normally, as seen in step 6.
What happens if the resource does not exist before apply but -replace is used?
Terraform will simply create the resource without destruction since there is no existing resource to replace, similar to skipping step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of aws_instance.example after step 4?
Aexisting
Bdestroyed
Cmarked for replacement
Dcreated
💡 Hint
Check the 'State After' column for step 4 in the execution_table.
At which step does Terraform detect the -replace flag?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look for the action 'Check -replace flag' in the execution_table.
If you remove the -replace flag, how would the execution_table change?
AResource would be destroyed but not recreated
BStep 3 would mark resource for replacement anyway
CStep 4 and 5 would be skipped; resource remains existing
DAll steps remain the same
💡 Hint
Refer to the concept_flow where no -replace flag leads to normal apply without replacement.
Concept Snapshot
terraform apply -replace=resource
Forces replacement of specified resource.
Destroys old resource, then creates new one.
Other resources apply normally.
Use to fix or refresh problematic resources.
Full Transcript
Terraform apply with the -replace flag forces Terraform to destroy and recreate a specified resource during the apply process. The flow starts with detecting the flag, marking the resource for replacement, destroying the old resource, creating a new one, then applying other changes normally. This is useful when you want to refresh or fix a resource without changing the entire infrastructure. Only the specified resource is replaced; others remain unchanged.