Terraform Module does not declare a provider warning

Deploying Terraform to Azure using a module and notice a warning similar to the below? In this blog post I will show how you can fix the warning

“Module module.storageaccount does not declare a provider named azurerm.storageaccount.
If you wish to specify a provider configuration for the module, add an entry for azurerm.storageaccount in the required_providers block within the module.”

Warning output:

│ Warning: Provider azurerm.storageaccount is undefined
│
│   on terraform.tf line 5, in module "storageaccount":
│    5:     azurerm.storageaccount = azurerm.storageaccount_nonprod
│
│ Module module.storageaccount does not declare a provider named azurerm.storageaccount.
│ If you wish to specify a provider configuration for the module, add an entry for azurerm.storageaccount in the required_providers block within the module.

The folder structure I will be using (note:- this is a dummy setup just to reflect the warning & how to fix)

Terraform-edit-remote-state
    └── component
       └── test
         └── terraform.tf
         └── providers.tf
    └── modules
       └── storageaccount
         └── main.tf

File setup

providers.tf

terraform {
  required_version = ">= 0.13.0"

  backend "local" {}
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.58.0"
    }
  }
}
provider "azurerm" {
  features {}
  skip_provider_registration = true
}

provider "azurerm" {
  alias = "storageaccount_nonprod"
  features {}
  subscription_id = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"
}


terraform.tf

module "storageaccount" {

  providers = {
    azurerm               = azurerm
    azurerm.storageaccount = azurerm.storageaccount_nonprod
  }

  source                  = "../../modules/storageaccount"
}

main.tf

data "azurerm_resource_group" "tamopsrg" {
provider = azurerm.storageaccount
  name     = "tamops"
}

# Create Storage Account
resource "azurerm_storage_account" "tamopssa" {
  name                     = "thaoartowqer"
  resource_group_name      = data.azurerm_resource_group.tamopsrg.name
  location                 = data.azurerm_resource_group.tamopsrg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Running the Terraform

As showed in providers.tf above, I am running the Terraform locally,

I want to use two providers, both azurerm & azurerm.storageaccount, if you review terraform.tf

  providers = {
    azurerm               = azurerm
    azurerm.storageaccount = azurerm.storageaccount_nonprod
  }

If I run a Terraform Plan you will notice the warnings being flagged at the bottom of the plan

Snippet from Terraform Plan showing the warnings

Plan: 1 to add, 0 to change, 0 to destroy.
╷
│ Warning: Provider azurerm is undefined
│
│   on terraform.tf line 4, in module "storageaccount":
│    4:     azurerm               = azurerm
│
│ Module module.storageaccount does not declare a provider named azurerm.
│ If you wish to specify a provider configuration for the module, add an entry for azurerm in the required_providers block within the module.
╵
╷
│ Warning: Provider azurerm.storageaccount is undefined
│
│   on terraform.tf line 5, in module "storageaccount":
│    5:     azurerm.storageaccount = azurerm.storageaccount_nonprod
│
│ Module module.storageaccount does not declare a provider named azurerm.storageaccount.
│ If you wish to specify a provider configuration for the module, add an entry for azurerm.storageaccount in the required_providers block within the module.
╵

The Resolution

Adding required_providers to my module folder

I created a file init.tf inside the storageaccount module, new file structure below

Terraform-edit-remote-state
    └── component
       └── test
         └── terraform.tf
         └── providers.tf
    └── modules
       └── storageaccount
         └── main.tf
         └── init.tf

init.tf

terraform {
  required_providers {
    azurerm = {
      source                = "hashicorp/azurerm"
      configuration_aliases = [azurerm.storageaccount]
    }
  }
}

Now if I run a Terraform Plan, no warnings is flagged!

Plan: 1 to add, 0 to change, 0 to destroy.

Awesome – hopefully this blog assists you, if you come across this warning 🙂

GitHub Repo here for code used in the above

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s