Introduction
This error occurs when Terraform cannot "find" the provider for a given resource that it is trying to take action on. The most common scenario is when a user has defined providers within a module (not recommended) and now wants to destroy an entire module and all the resources associated with it. Here is an example.
Bad configuration
In this example, the user defines the AWS provider within the child module (see "Figure C" below). This will technically work. However, they will encounter problems if they attempt to destroy module.test (see "Figure D").
We can explain this by looking at the state file in Figure E. Let's look at the full error message, "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." In the state file, you can see "provider" is defined and associated with this resource.
To work around this, you will need to comment back in module.test and then comment out all the resources in ./modules/src.tf, but you must keep the AWS provider definition. Then execute apply.
Finally, you will need to move the provider definition to the root (see Figure F and G).
Figure A: Filesystem
.
├── main.tf
└── modules
└── src.tf
Figure B: Root module defines the Terraform block and calls the child module
# ./main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.92.0"
}
}
}
module "test {
source = "./modules"
}
Figure C: Child module source code incorrectly defines the AWS provider
# ./modules/src.tf
# BAD CHOICE
provider "aws" {
region = "us-east-2"
}
resource "aws_ebs_volume" "e" {
availability_zone = "us-east-2a"
size = 10
}
Figure D: Commenting out the module and running an apply gives an error
# ./main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.92.0"
}
}
}
# module "test {
# source = "./modules"
# }
/*
On apply, Terraform will error with
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.
*/
Figure E: State file references the module that was commented out
// ...
"resources": [
{
"module": "module.test",
"mode": "managed",
"type": "aws_ebs_volume",
"name": "e",
"provider": "module.test.provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
// ...
Figure F: Correct configuration for the root module
# ./main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.92.0"
}
}
}
# CORRECT
provider "aws" {
region = "us-east-2"
}
module "test {
source = "./modules"
}
Figure G: Correct Configuration for the child module (module.test source code)
# ./modules/src.tf
resource "aws_ebs_volume" "e" {
availability_zone = "us-east-2a"
size = 10
}
Additional information
If you continue to experience the issues after following the guides, please contact HashiCorp Support to request for further assistance.