Error: Provider configuration not present while deleting azure app service

Hello…!!

I am trying to delete one of the app service created using terraform, So Current directory structure is as follows

The Code Hierarchy is as follows

  • module directory - containing list of modules

    • app_service_plan

    • app_service

      • provider.tf
provider "azurerm" {
  version = "3.54.0"
  features {}
  skip_provider_registration = true
}

  * app_service.tf
resource "azurerm_windows_web_app" "webapp" {
  name                = "app_service"
  location            = var.location
  resource_group_name = var.resource_group_name
  service_plan_id     = var.app_services_plan_id
  site_config{...}
}
  • eastus - root directory to create azure resources

    • main.tf
      The app service is module is called and all other app service configuration is also passed.
      We have around 3 app service defined in similar way, the issue is now we need to delete one of the app service since team isnt using any more.
module "app_service_xyz" {
  source = "../../modules/app_service"
.....
}
* provider.tf
provider "azurerm" {
  version = "3.54.0"
  features {}
  skip_provider_registration = true
}

Ideally when we remove / comment the configuration from main.tf it should destory the app service and dependent services. but in my case I am getting below error
but I am getting below error.

Error
To work with module.app_service_xyz.azurerm_windows_web_app.webapp its
original provider configuration at
module.app_service_xyz.provider[“Terraform Registry”] is
required, but it has been removed. This occurs when a provider configuration
is removed while objects created by that provider still exist in the state.
Re-add the provider configuration to destroy
module.app_service_xyz.azurerm_windows_web_app.webapp, after which you can
remove the provider configuration again.

Does any one has any idea, it would be very helpful. Dint found any easy or correct step guide to resolve this issue…

In modern Terraform, it is strongly discouraged to configure providers inside child modules. The problem is that you have the provider "azurerm" block inside module/app_service instead of only within the root module - which seems to be eastus based on what you have said above.

Although in your case, it seems that you have the same configuration in provider "azurerm" blocks in both directories, Terraform doesn’t assume this is the case.

It remembers that it created the app_service_xyz resources using an azurerm provider that was defined in that directory, and thinks it needs that provider configuration to still exist, in order to destroy them.

Since in your particular case, the azurerm configuration in eastus is the same as in modules/app_service, it looks like it might be sufficient to just remove the provider.tf file from inside module/app_service directory - then run a terraform apply to update the Terraform state so the existing resources get re-linked to the root provider configuration.

After that, you could only then remove the app_service_xyz module block, and it should probably then be removed as intended.

It din’t worked that way., I had to point out to existing module update the state file and create alias in provider.tf in same app_service module update the configuration and then delete the app_service_xyz module block.