Problem
When attempting to remove a module from your Terraform configuration, you may encounter the following error during terraform apply:
Error: Provider configuration not present To work with module.test.aws_ebs_volume.e (orphan) its original provider configuration at module.test.provider["registry.terraform.io/hashicorp/aws"] is required, but it has been removed.
This error indicates that Terraform cannot find the provider configuration for a resource that it needs to destroy because the configuration was removed along with the module definition.
Cause
This error occurs when a provider is configured within a child module instead of the root module. When you comment out or remove the module block from your configuration, Terraform also loses access to the provider block defined within it. However, the resources created by that module still exist in the state file and are associated with that provider configuration. Terraform cannot destroy these "orphaned" resources without their original provider configuration.
Example of an Incorrect Configuration
Consider the following file structure.
.
├── main.tf
└── modules
└── src.tfHere, the provider is incorrectly defined within the child module (./modules/src.tf).
main.tf - Root Module
# ./main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.92.0"
}
}
}
# The module block is commented out to trigger the error.
# module "test" {
# source = "./modules"
# }./modules/src.tf - Child Module
# ./modules/src.tf
# INCORRECT: Provider is defined inside the module.
provider "aws" {
region = "us-east-2"
}
resource "aws_ebs_volume" "e" {
availability_zone = "us-east-2a"
size = 10
}Because the aws provider is defined inside the test module, removing the module "test" block makes the provider configuration unavailable, causing the error.
Solutions
To resolve this issue, you must first perform a workaround to clean the state and then correct your configuration to follow best practices.
Solution 1: Workaround to Repair State
This procedure allows Terraform to destroy the resources by temporarily restoring the provider configuration.
- In your root module (
main.tf), uncomment themoduleblock to make it active again. In your child module's source file (
./modules/src.tf), comment out theresourceblocks but leave theproviderblock active.# ./modules/src.tf provider "aws" { region = "us-east-2" } # resource "aws_ebs_volume" "e" { # availability_zone = "us-east-2a" # size = 10 # }- Run
terraform apply. Terraform will see the provider configuration is present but the resources are gone, and it will remove the resources from the state file.
Solution 2: Correct the Provider Configuration
To prevent this error from happening again, always define providers in the root module.
Move the
providerblock from the child module (./modules/src.tf) to the root module (main.tf).main.tf- Corrected# ./main.tf terraform { required_providers { aws = { source = "hashicorp/aws" version = "5.92.0" } } } # CORRECT: Provider is defined in the root module. provider "aws" { region = "us-east-2" } module "test" { source = "./modules" }./modules/src.tf- Corrected# ./modules/src.tf # The provider block has been removed from here. resource "aws_ebs_volume" "e" { availability_zone = "us-east-2a" size = 10 }
With this corrected structure, you can now safely comment out or remove the module "test" block from main.tf and run terraform apply. Terraform will successfully destroy the resources because the provider configuration remains available in the root module.