Terraform apply -replace flag - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Terraform's -replace flag, we want to understand how the time to apply changes grows as we replace more resources.
We ask: How does replacing resources affect the number of operations Terraform performs?
Analyze the time complexity of applying Terraform with the -replace flag on multiple resources.
terraform apply -replace=module.example.resource[0] \
-replace=module.example.resource[1] \
-replace=module.example.resource[2]
This command forces Terraform to destroy and recreate the specified resources during apply.
Terraform performs these repeated operations for each replaced resource:
- Primary operation: Destroy and recreate the specified resource via API calls.
- How many times: Once per resource listed with
-replace.
As you increase the number of resources to replace, Terraform makes more API calls to destroy and recreate each one.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 destroy and 10 create calls |
| 100 | About 100 destroy and 100 create calls |
| 1000 | About 1000 destroy and 1000 create calls |
Pattern observation: The number of operations grows directly with the number of replaced resources.
Time Complexity: O(n)
This means the time to apply changes grows linearly with how many resources you replace.
[X] Wrong: "Replacing multiple resources happens all at once, so time stays the same."
[OK] Correct: Each resource replacement requires separate destroy and create operations, so time grows with the number of replacements.
Understanding how Terraform operations scale helps you plan infrastructure changes efficiently and communicate clearly about deployment times.
"What if we replaced a module with many resources at once instead of individual resources? How would the time complexity change?"
Practice
-replace flag do when used with terraform apply?Solution
Step 1: Understand the purpose of the -replace flag
The -replace flag tells Terraform to destroy and recreate a specific resource during apply.Step 2: Compare with other apply behaviors
Normally, Terraform updates resources in place if possible, but -replace forces full replacement of the targeted resource.Final Answer:
Forces Terraform to destroy and recreate a specific resource -> Option CQuick Check:
-replace flag = force resource replacement [OK]
- Thinking -replace updates resources in place
- Confusing -replace with plan-only mode
- Assuming it affects all resources
aws_instance.example using Terraform apply?Solution
Step 1: Recall the correct flag syntax
The correct syntax uses an equals sign with the resource address: -replace=resource_address.Step 2: Check the resource address format
The resource address isaws_instance.example, so the correct command isterraform apply -replace=aws_instance.example.Final Answer:
terraform apply -replace=aws_instance.example -> Option AQuick Check:
Correct syntax uses '=' with resource address [OK]
- Using space instead of '=' after -replace
- Adding extra flags like --replace
- Appending .id unnecessarily
terraform apply -replace=aws_s3_bucket.mybucketWhat will happen to the resource
aws_s3_bucket.mybucket during apply?Solution
Step 1: Understand the effect of -replace on a resource
The -replace flag forces Terraform to destroy and recreate the specified resource during apply.Step 2: Apply this to aws_s3_bucket.mybucket
Since the command targetsaws_s3_bucket.mybucket, Terraform will destroy and then recreate this bucket.Final Answer:
It will be destroyed and recreated -> Option DQuick Check:
-replace causes destroy and recreate [OK]
- Thinking it updates resource without destruction
- Assuming it skips the resource
- Confusing apply with plan
terraform apply -replace=aws_instance.web but get an error: Invalid resource address. What is the most likely cause?Solution
Step 1: Analyze the error message
An 'Invalid resource address' error means Terraform cannot find the resource in the state or the address is wrong.Step 2: Check resource existence and spelling
Most likely, the resourceaws_instance.webis misspelled or not present in the current Terraform state.Final Answer:
The resource name is misspelled or does not exist in the state -> Option AQuick Check:
Invalid address = wrong or missing resource name [OK]
- Assuming -replace flag is invalid for resource types
- Thinking terraform init fixes resource address errors
- Using space instead of '=' causes different errors
aws_instance.web and aws_security_group.sg, in a single apply command. Which is the correct way to do this?Solution
Step 1: Understand multiple -replace usage
Terraform allows multiple -replace flags, each specifying one resource to replace.Step 2: Evaluate each option
terraform apply -replace=aws_instance.web -replace=aws_security_group.sg uses two separate -replace flags correctly. terraform apply -replace=aws_instance.web,aws_security_group.sg uses a comma which is invalid. Options C and D use incorrect syntax without '=' or with spaces.Final Answer:
terraform apply -replace=aws_instance.web -replace=aws_security_group.sg -> Option BQuick Check:
Use multiple -replace flags for multiple resources [OK]
- Trying to list multiple resources with commas
- Using spaces instead of '=' after -replace
- Combining resources in one -replace flag
