How to fix Error: Resource not found in Terraform
This error occurs when Terraform can't locate a resource that exists in your state file but has been deleted manually or moved outside of Terraform. Fix it by syncing your state file with actual infrastructure using terraform refresh, terraform import, or terraform state commands.
Quick fix
Fixes Error: Resource not found
# Option 1: Full refresh (Terraform 0.15.4+, recommended)
terraform apply -refresh-only
terraform plan
# Option 2: Legacy refresh command
terraform refresh
terraform plan- Check if the resource actually exists in your cloud provider
- Refresh your Terraform state to sync with reality
- Remove the resource from state if it no longer exists
- Import the resource if it exists but isn't in state
# Option 1: Full refresh (Terraform 0.15.4+, recommended)terraform apply -refresh-onlyterraform plan# Option 2: Legacy refresh commandterraform refreshterraform planError: Resource not found
What this error means
This error means Terraform is trying to manage a resource that it believes should exist according to your state file, but the resource no longer exists in your cloud provider (AWS, GCP, Azure, etc.). This typically happens when: 1. **A resource was manually deleted** outside of Terraform without updating the state file 2. **State file is out of sync** with actual infrastructure due to external changes 3. **Module refactoring** changed resource addresses, making Terraform think they're new or missing 4. **Region or project mismatches** prevent Terraform from finding the resource 5. **Failed Terraform runs** left the state file in an incomplete state The error is Terraform's way of alerting you that there's a discrepancy between the desired state (in your configuration) and the actual state of your infrastructure.
Typical symptoms
- Terraform apply or destroy fails with 'Resource not found' message
- Error occurs during terraform destroy when infrastructure was already removed
- Error appears after manually deleting cloud resources outside of Terraform
- Terraform plan shows resources that don't exist in the cloud provider
- Error happens after module refactoring or moving resources between modules
- Intermittent failures suggesting race conditions in resource creation/deletion
- Error references wrong region or project than where resources actually exist
Common causes
- Resource was manually deleted in the AWS/GCP console without removing it from Terraform state
- Infrastructure was destroyed outside of Terraform by another team member or automated process
- Module refactoring changed resource addresses, causing Terraform to lose track of existing resources
- Provider configuration (region, project, credentials) doesn't match where the resource actually exists
- State file wasn't pushed after a failed Terraform run, leaving it out of sync
- Resource exists in a different region/project than specified in your Terraform configuration
- Manual state file edits or imports created inconsistent references
- for_each or count loops changed, altering how Terraform addresses resources
How to fix "Error: Resource not found"
First, verify whether the resource still exists in your AWS/GCP console:
For AWS:
- Open the AWS Console
- Navigate to the resource type (EC2, S3, RDS, etc.)
- Search for the resource by ID or name mentioned in the error
- Check that you're in the correct region
For GCP:
- Open the Google Cloud Console
- Verify you're in the correct project
- Check the resource list for the type mentioned in the error
- Confirm the resource hasn't been deleted
If the resource no longer exists, proceed to Step 2. If it does exist, check Step 4 for configuration mismatches.
If the resource was manually deleted, synchronize your state file with actual infrastructure:
# Option 1: Full refresh (Terraform 0.15.4+, recommended)
terraform apply -refresh-only
terraform plan
# Option 2: Legacy refresh command
terraform refresh
terraform planThis reads the current state of all resources from your cloud provider and updates your state file. If Terraform confirms the resource no longer exists, the state will be updated accordingly.
If the resource has been permanently deleted and no longer exists in the cloud, remove it from your Terraform state:
terraform state rm <resource_address>For example:
terraform state rm aws_instance.web_server
terraform state rm module.networking.aws_security_group.mainAfter removing it, run terraform plan to verify the resource is no longer referenced.
If the resource exists in the cloud but Terraform can't find it (perhaps after module changes), import it:
terraform import <resource_address> <resource_id>Examples:
# AWS EC2 instance
terraform import aws_instance.web i-1234567890abcdef0
# AWS S3 bucket
terraform import aws_s3_bucket.mybucket mybucket
# GCP Compute instance
terraform import google_compute_instance.default us-central1-a/my-instanceRun terraform plan after importing to verify no changes are needed.
Ensure your Terraform provider is configured for the correct region/project:
# AWS
provider "aws" {
region = "us-east-1" # Matches where your resource exists
}
# GCP
provider "google" {
project = "my-project"
region = "us-central1"
}If you changed regions or projects, update your provider configuration and run:
terraform plan
terraform applyIf you refactored modules and Terraform can't find resources, move them in state:
# Move a resource to a new address
terraform state mv aws_instance.old_address aws_instance.new_address
# Move a resource into a module
terraform state mv aws_s3_bucket.bucket module.storage.aws_s3_bucket.bucketList all state resources first to understand current addresses:
terraform state listAfter moving resources, run terraform plan to verify no changes are required.
Advanced notes
Preventing this error in the future:
1. Always use Terraform for changes: Never manually delete or modify cloud resources. Always use terraform destroy or terraform apply to make changes.
2. Lock state files: Use Terraform Cloud, S3 with DynamoDB locking, or other backends to prevent concurrent modifications that can corrupt state.
3. Review state changes: Use terraform plan before terraform apply to catch issues early.
4. Tag and document resources: Add tags/labels to all resources linking them to Terraform so manual deletions are less likely.
5. For CI/CD pipelines: Always check that resource IDs and region configurations are correct in your deployment scripts.
6. Partial failures: If a Terraform run partially fails (some resources created, some not), manually verify the state and use terraform refresh before retrying.
When importing resources with for_each:
If you have resources using for_each loops, escape resource addresses carefully:
# Resource address with for_each using the key "prod-server"
terraform import 'aws_instance.servers["prod-server"]' i-1234567890abcdef0Consult Terraform's state documentation for complex scenarios.
Frequently asked questions
- What does "Error: Resource not found" mean?
- ). This typically happens when: 1. A resource was manually deleted outside of Terraform without updating the state file 2.
- How do I fix "Error: Resource not found"?
- This error occurs when Terraform can't locate a resource that exists in your state file but has been deleted manually or moved outside of Terraform. Fix it by syncing your state file with actual infrastructure using terraform refresh, terraform import, or terraform state commands.
- What causes "Error: Resource not found"?
- Resource was manually deleted in the AWS/GCP console without removing it from Terraform state Infrastructure was destroyed outside of Terraform by another team member or automated process Module refactoring changed resource addresses, causing Terraform to lose track of existing resources
- Is the "Error: Resource not found" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.