Error: Provider configuration not present with azurerm provider

I’ve rafactored my code to use modules and when I attempt to apply a module leveraging the azurerm provider in order to create a service principal and role assingments, I’m getting the following:

Error: Provider configuration not present

To work with
module.azure_arc_ds_controller.azurerm_role_assignment.contributor its
original provider configuration at
module.azure_arc_ds_controller.provider[“registry.terraform.io/hashicorp/azurerm”].azure_rm
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.azure_arc_ds_controller.azurerm_role_assignment.contributor, after
which you can remove the provider configuration again.

Error: Provider configuration not present

To work with
module.azure_arc_ds_controller.azurerm_role_assignment.monitoring_metrics_publisher
its original provider configuration at
module.azure_arc_ds_controller.provider[“registry.terraform.io/hashicorp/azurerm”].azure_rm
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.azure_arc_ds_controller.azurerm_role_assignment.monitoring_metrics_publisher,
after which you can remove the provider configuration again.

Error: Provider configuration not present

To work with module.azure_arc_ds_controller.data.azurerm_subscription.primary
its original provider configuration at
module.azure_arc_ds_controller.provider[“registry.terraform.io/hashicorp/azurerm”].azure_rm
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.azure_arc_ds_controller.data.azurerm_subscription.primary, after which
you can remove the provider configuration again.

The little I have been able to glean about this suggests that not enough info exists in the tfstate file with which terraform can delete these resources, I’m confident that these resources do not exist in Azure and I also there is nothing in the tfstate files, I’ve gone through the Azure portal and I do not seem to have any resources in existence associated with the module, as per module best practice, I’ve moved all providers into the main.tf in the root module, but I have the following in the child module:

data "azurerm_subscription" "primary" {
  provider = azurerm.azure_rm
}

I wonder if this might in some way be related to the problem.

That is the problem, I moved the providers back into the main.tf for the child module and it works, given that its a bad practice to include providers in child modules, what would be the easiest way to remove:

data "azurerm_subscription" "primary" {
  provider = azurerm.azure_rm
}

from the child module ?

Hi @chrisadkin

In this case I think you should be able to proceed by removing the data source from the module first, while still leaving the provider configuration intact. Once you apply that configuration to remove the data source from the state, you should be able to remove the provider as well.

I’ve removed the data source from the module and this has made no difference unfortunately.

There is likely something else in the module which requires the configuration if that is still the case. If you are getting that error when trying to remove the module, it means something else still exists in the state requires the provider. The output above lists some of the resources like module.azure_arc_ds_controller.azurerm_role_assignment.monitoring_metrics_publisher which require a provider configuration to be present until the resource is removed from the state.

If you are certain the actual resources no longer exist, you can manually remove them from the state rather than going through the process of destroying the resources in terraform.