How to Use Terraform Modules - Build Reusable Infrastructure
Create and use Terraform modules for reusable infrastructure code. Covers module structure, inputs, outputs, versioning, and the Terraform Registry.
Terraform
Destroy individual Terraform resources without affecting the rest. Covers -target flag, state rm, replace, and lifecycle prevent_destroy.
Use terraform destroy -target=RESOURCE_ADDRESS to destroy a single resource. For example: terraform destroy -target=aws_instance.web. This leaves all other resources untouched.
# Destroy one specific resource
terraform destroy -target=aws_instance.web
# Destroy a module instance
terraform destroy -target=module.app
# Destroy an indexed resource
terraform destroy -target='aws_subnet.private[0]'
terraform destroy -target='aws_subnet.private["us-east-1a"]'
# Preview first (plan)
terraform plan -destroy -target=aws_instance.web# 1. Delete or comment out the resource block
# resource "aws_instance" "web" { ... }
# 2. Apply — Terraform destroys the removed resource
terraform applyThis is the cleanest approach — config and state stay in sync.
# Remove from state WITHOUT destroying the cloud resource
terraform state rm aws_instance.web
# The resource stays in AWS but Terraform forgets about it
# Use this when you want to stop managing a resource# Terraform 1.4+: replace a resource (destroy old, create new)
terraform apply -replace=aws_instance.web
# Older versions: taint then apply
terraform taint aws_instance.web
terraform apply# Tell Terraform to forget this resource without destroying it
removed {
from = aws_instance.web
lifecycle {
destroy = false
}
}resource "aws_db_instance" "main" {
# ...
lifecycle {
prevent_destroy = true # terraform destroy will fail for this resource
}
}terraform destroy -target=aws_db_instance.main
# Error: Instance cannot be destroyed
# To override: remove prevent_destroy, then destroy| Goal | Command |
|---|---|
| Destroy one resource | terraform destroy -target=aws_instance.web |
| Destroy a module | terraform destroy -target=module.app |
| Stop managing (keep in cloud) | terraform state rm aws_instance.web |
| Force recreate | terraform apply -replace=aws_instance.web |
| Remove from config | Delete block, then terraform apply |
plan -destroy -target first — review what will be destroyedprevent_destroy on databases, S3 buckets, and other critical resources-target is for surgical operations — don't use it as your normal workflowterraform destroy -target is the quickest way to remove a single resource. For a cleaner workflow, remove the resource block from config and run terraform apply. Use terraform state rm when you want Terraform to stop managing a resource without destroying it in the cloud.
Create and use Terraform modules for reusable infrastructure code. Covers module structure, inputs, outputs, versioning, and the Terraform Registry.
Learn when to use Terraform provisioners and when to avoid them. Covers local-exec, remote-exec, file provisioner, null_resource, and better alternatives.
Learn the best way to organize Terraform projects. Covers file structure, modules, environments, and naming conventions for scalable infrastructure code.
Master Terraform lifecycle meta-arguments. Covers prevent_destroy, create_before_destroy, ignore_changes, and replace_triggered_by with examples.