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
Terraform state rm for removing resources
📖 Scenario: You are managing cloud infrastructure using Terraform. Sometimes, you need to remove a resource from Terraform's state without deleting the actual resource in the cloud. This helps when you want Terraform to stop managing that resource but keep it running.
🎯 Goal: Learn how to use the terraform state rm command to remove a resource from Terraform's state file safely.
📋 What You'll Learn
Create a Terraform configuration with one AWS S3 bucket resource
Initialize Terraform in the project directory
Add a configuration variable to hold the resource address
Use the terraform state rm command with the resource address to remove it from state
💡 Why This Matters
🌍 Real World
Cloud engineers often need to remove resources from Terraform state to stop managing them without deleting the actual cloud resources. This is useful during migrations or manual changes.
💼 Career
Knowing how to safely manipulate Terraform state is essential for infrastructure as code roles, ensuring smooth operations and avoiding accidental resource deletions.
Progress0 / 4 steps
1
Create a Terraform configuration with an AWS S3 bucket
Create a file named main.tf and write a Terraform resource block for an AWS S3 bucket named example_bucket with the bucket name my-terraform-bucket-12345. Use the resource type aws_s3_bucket and resource name example_bucket.
Terraform
Hint
Use the resource keyword, specify aws_s3_bucket as the type, and example_bucket as the name. Set the bucket attribute to the exact bucket name.
2
Initialize Terraform in the project directory
Run the command terraform init in your terminal to initialize the Terraform working directory and download the AWS provider plugin.
Terraform
Hint
Open your terminal and type terraform init to prepare Terraform for managing your infrastructure.
3
Add a variable to hold the resource address
In the main.tf file, create a Terraform variable named resource_address with the default value set to aws_s3_bucket.example_bucket. This variable will hold the address of the resource you want to remove from the state.
Terraform
Hint
Use the variable block with type = string and set default to the resource address string.
4
Remove the resource from Terraform state using terraform state rm
Use the command terraform state rm ${var.resource_address} in your terminal to remove the AWS S3 bucket resource from Terraform's state file without deleting the actual bucket.
Terraform
Hint
In your terminal, type terraform state rm followed by the variable reference ${var.resource_address} to remove the resource from state.
Practice
(1/5)
1. What does the terraform state rm command do?
easy
A. Deletes the resource from the cloud provider and Terraform state
B. Removes a resource from Terraform's state without deleting the actual resource
C. Updates the resource configuration in Terraform files
D. Creates a new resource and adds it to the state
Solution
Step 1: Understand the purpose of terraform state rm
This command tells Terraform to stop tracking a resource by removing it from the state file.
Step 2: Recognize it does not delete the actual resource
The real resource remains intact in the cloud; only Terraform forgets it.
Final Answer:
Removes a resource from Terraform's state without deleting the actual resource -> Option B
Quick Check:
State removal only forgets resource [OK]
Hint: Remember: state rm forgets resource, does not delete it [OK]
Common Mistakes:
Thinking it deletes the actual resource
Confusing it with terraform destroy
Assuming it updates resource configuration
2. Which of the following is the correct syntax to remove a resource named aws_instance.example from Terraform state?
easy
A. terraform rm state aws_instance.example
B. terraform remove aws_instance.example
C. terraform state rm aws_instance.example
D. terraform state delete aws_instance.example
Solution
Step 1: Recall the correct command structure
The command to remove a resource from state is terraform state rm <resource_name>.
Step 2: Match the resource name format
The resource name is aws_instance.example, so the full command is terraform state rm aws_instance.example.
Final Answer:
terraform state rm aws_instance.example -> Option C
Quick Check:
Correct command syntax [OK]
Hint: Use 'terraform state rm' followed by resource name [OK]
Common Mistakes:
Swapping 'rm' and 'state' keywords
Using 'remove' or 'delete' instead of 'rm'
Incorrect command order
3. Given the following Terraform state command:
terraform state rm aws_s3_bucket.my_bucket
What will happen after running this command?
medium
A. Terraform will remove the S3 bucket from state but the bucket remains in AWS
B. Terraform will update the S3 bucket configuration in the state
C. Terraform will delete the S3 bucket from AWS and remove it from state
D. Terraform will show an error because the command is incomplete
Solution
Step 1: Understand the effect of terraform state rm
This command removes the resource from Terraform's state file only.
Step 2: Recognize the real resource remains untouched
The actual S3 bucket in AWS is not deleted or changed by this command.
Final Answer:
Terraform will remove the S3 bucket from state but the bucket remains in AWS -> Option A
Quick Check:
State removal keeps resource intact [OK]
Hint: State rm forgets resource, does not delete it in cloud [OK]
Common Mistakes:
Assuming the resource is deleted from AWS
Thinking the state file is updated with new config
Believing the command is incomplete
4. You ran terraform state rm aws_instance.web but later realized you still want Terraform to manage this instance. What is the best way to fix this?
medium
A. Run terraform import aws_instance.web <instance_id> to re-add it to state
B. Run terraform state add aws_instance.web to add it back
C. Run terraform state rm aws_instance.web again to undo
D. Delete the instance manually and recreate it
Solution
Step 1: Understand that terraform state rm only removes from state
Once removed, Terraform no longer tracks the resource.
Step 2: Use terraform import to re-add the existing resource to state
This command links the real resource back to Terraform's state file.
Final Answer:
Run terraform import aws_instance.web <instance_id> to re-add it to state -> Option A
Quick Check:
Import command restores resource to state [OK]
Hint: Use terraform import to re-add removed resources [OK]
Common Mistakes:
Trying to use a non-existent 'state add' command
Running state rm again expecting undo
Deleting resource unnecessarily
5. You want to stop managing a resource google_compute_instance.vm1 with Terraform but keep it running in your cloud. Which sequence of commands achieves this safely?
hard
A. Run terraform state rm google_compute_instance.vm1 then manually delete the resource
B. Run terraform destroy -target=google_compute_instance.vm1 to remove it from cloud and state
C. Remove the resource block from Terraform files and run terraform apply
D. Run terraform state rm google_compute_instance.vm1 only to remove it from state and keep it running
Solution
Step 1: Use terraform state rm to remove resource from state
This stops Terraform from managing the resource but does not delete it.
Step 2: Avoid deleting the resource or removing config to keep it running
Deleting or removing config and applying would delete or recreate the resource.
Final Answer:
Run terraform state rm google_compute_instance.vm1 only to remove it from state and keep it running -> Option D
Quick Check:
State rm forgets resource, keeps it running [OK]
Hint: State rm removes from Terraform control, resource stays alive [OK]
Common Mistakes:
Deleting resource after state rm
Using terraform destroy which deletes resource
Removing config without state rm causes resource deletion