Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Terraform apply with the -replace Flag
📖 Scenario: You are managing cloud infrastructure using Terraform. Sometimes, you need to force Terraform to recreate a specific resource without affecting others. This is useful when a resource is in a bad state or needs to be refreshed.
🎯 Goal: Learn how to use the terraform apply -replace command to recreate a specific resource while applying changes.
📋 What You'll Learn
Create a Terraform configuration with a resource
Add a variable to specify the resource to replace
Write a command to apply changes with the -replace flag
Complete the Terraform command with the correct resource address
💡 Why This Matters
🌍 Real World
In real cloud projects, sometimes resources get corrupted or need to be recreated without changing the entire infrastructure. The '-replace' flag helps target only those resources.
💼 Career
Cloud engineers and DevOps professionals use Terraform and the '-replace' flag to manage infrastructure safely and efficiently, minimizing downtime and errors.
Progress0 / 4 steps
1
Create a Terraform configuration with an AWS S3 bucket
Create a Terraform file named main.tf with a resource called aws_s3_bucket.my_bucket that creates an S3 bucket named my-unique-bucket-12345.
Terraform
Hint
Use the resource block with type aws_s3_bucket and name my_bucket. Set the bucket attribute to the exact bucket name.
2
Add a variable to specify the resource to replace
Add a variable called replace_resource of type string with a default value of "aws_s3_bucket.my_bucket" in a file named variables.tf.
Terraform
Hint
Use the variable block with the name replace_resource. Set the type to string and default to the resource address.
3
Write the Terraform apply command with the -replace flag
Write a shell command that runs terraform apply and uses the -replace flag with the value of the variable replace_resource.
Terraform
Hint
Use the command terraform apply -replace="${var.replace_resource}" to force replacement of the specified resource.
4
Complete the Terraform command with the correct resource address
Modify the terraform apply command to replace the resource aws_s3_bucket.my_bucket explicitly by writing terraform apply -replace="aws_s3_bucket.my_bucket".
Terraform
Hint
Write the full command terraform apply -replace="aws_s3_bucket.my_bucket" to force recreate the bucket resource.
Practice
(1/5)
1. What does the -replace flag do when used with terraform apply?
easy
A. Only plans changes without applying them
B. Skips the creation of new resources
C. Forces Terraform to destroy and recreate a specific resource
D. Updates all resources without destroying any
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 C
Quick Check:
-replace flag = force resource replacement [OK]
Hint: Remember: -replace means rebuild that resource only [OK]
Common Mistakes:
Thinking -replace updates resources in place
Confusing -replace with plan-only mode
Assuming it affects all resources
2. Which of the following is the correct syntax to replace a resource named aws_instance.example using Terraform apply?
easy
A. terraform apply -replace=aws_instance.example
B. terraform apply --replace aws_instance.example
C. terraform apply -replace aws_instance.example
D. terraform apply -replace=aws_instance.example.id
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 is aws_instance.example, so the correct command is terraform apply -replace=aws_instance.example.
Final Answer:
terraform apply -replace=aws_instance.example -> Option A
Quick Check:
Correct syntax uses '=' with resource address [OK]
Hint: Use '=' directly after -replace with resource name [OK]
Common Mistakes:
Using space instead of '=' after -replace
Adding extra flags like --replace
Appending .id unnecessarily
3. Given this Terraform command: terraform apply -replace=aws_s3_bucket.mybucket What will happen to the resource aws_s3_bucket.mybucket during apply?
medium
A. It will be updated in place without destruction
B. It will be ignored and left unchanged
C. Terraform will only plan changes but not apply
D. It will be destroyed and recreated
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 targets aws_s3_bucket.mybucket, Terraform will destroy and then recreate this bucket.
Final Answer:
It will be destroyed and recreated -> Option D
Quick Check:
-replace causes destroy and recreate [OK]
Hint: Replace means destroy then create that resource [OK]
Common Mistakes:
Thinking it updates resource without destruction
Assuming it skips the resource
Confusing apply with plan
4. You run terraform apply -replace=aws_instance.web but get an error: Invalid resource address. What is the most likely cause?
medium
A. The resource name is misspelled or does not exist in the state
B. The -replace flag cannot be used with aws_instance resources
C. You forgot to run terraform init before apply
D. The command requires a space instead of '=' after -replace
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 resource aws_instance.web is misspelled or not present in the current Terraform state.
Final Answer:
The resource name is misspelled or does not exist in the state -> Option A
Quick Check:
Invalid address = wrong or missing resource name [OK]
Hint: Check resource name spelling and state presence first [OK]
Common Mistakes:
Assuming -replace flag is invalid for resource types
Using space instead of '=' causes different errors
5. You want to replace two resources, aws_instance.web and aws_security_group.sg, in a single apply command. Which is the correct way to do this?
hard
A. terraform apply -replace=aws_instance.web,aws_security_group.sg
B. terraform apply -replace=aws_instance.web -replace=aws_security_group.sg
C. terraform apply -replace=aws_instance.web aws_security_group.sg
D. terraform apply -replace aws_instance.web -replace aws_security_group.sg
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 B
Quick Check:
Use multiple -replace flags for multiple resources [OK]
Hint: Use one -replace= per resource to replace multiple [OK]