Terraform returns 'missing provider' when validating

192 Views Asked by At

My issue

I have a Terraform configuration. But when I run:

terraform validate

I get this error message:

Error: missing provider module.sqlServer.provider["registry.terraform.io/hashicorp/azurerm"].dns

I am using Terraform 1.6.3

I have no idea what to do. Can anyone help me?

Test project

It can easily be reproduced with this project.

First tree folders:

enter image description here

In the root, the sqlserver.tf file:

module "sqlServer" {
  source              = "./modules/sqlServers"
  deployPrivateLink   = true
  resource_group_name = "rg01"
}


provider "azurerm" {
  tenant_id       = "123654"
  subscription_id = "852459"

  skip_provider_registration = true

  features {
  }
}

provider "azurerm" {
  alias                      = "dns"
  skip_provider_registration = true
  features {}
  subscription_id = "58e415dc"
}

terraform {
  required_providers {
    azurerm = {
      source                = "hashicorp/azurerm"
      version               = "=3.80.0"
      configuration_aliases = [azurerm.dns]
    }
  }
}

In sqlServers module, the main-sql.tf file:

resource "azurerm_mssql_server" "primary" {
  name                          = "myfdlmdb"
  resource_group_name           = var.resource_group_name
  location                      = "westeurope"
  administrator_login           = "dba_admin"
  administrator_login_password  = "azerty456!"
  minimum_tls_version           = "1.2"
  version                       = "12.0"
  public_network_access_enabled = true
}

module "privateLink_primary" {
  count               = var.deployPrivateLink ? 1 : 0 # deploy or not
  source              = "../private_endpoints"
  resource_group_name = var.resource_group_name
  target_resource_id  = azurerm_mssql_server.primary.id
  providers = {
    azurerm = azurerm.dns
  }
}

terraform {
  required_providers {
    azurerm = {
      source                = "hashicorp/azurerm"
      version               = "=3.80.0"
    }
  }
}

variable "deployPrivateLink" {
  type = bool
}

variable "resource_group_name" {
  type = string
}

In private_endpoints module, the main.pe.tf file:

resource "azurerm_private_endpoint" "pe" {
  name                = "myfdlmdbendpoint-pep"
  location            = "westeurope"
  resource_group_name = var.resource_group_name
  subnet_id           = data.azurerm_subnet.subnet.id


  private_service_connection {
    name                           = "myfdlmdbendpoint-psc"
    private_connection_resource_id = var.target_resource_id
    is_manual_connection           = false
  }
}

resource "azurerm_private_dns_a_record" "a_record" {
  name                = "foo"
  resource_group_name = var.resource_group_name
  zone_name           = "privatelink.database.windows.net"
  ttl                 = 10
  records             = [azurerm_private_endpoint.pe.private_service_connection[0].private_ip_address]
  provider            = azurerm.dns
}

data "azurerm_subnet" "subnet" {
  name                 = "name"
  virtual_network_name = "vnm"
  resource_group_name  = "rgn"
}

terraform {
  required_providers {
    azurerm = {
      source                = "hashicorp/azurerm"
      version               = "=3.80.0"
    }
  }
}

variable "resource_group_name" {
  type = string
}

variable "target_resource_id" {
  type = string
}

Important point

Deployment of privateLink_primary should be configurable by a feature flag pattern.

This is why I can't add provider block into privateLink_primary. This is not supported by Terraform in this case.

1

There are 1 best solutions below

0
On

Ok, I found the solution.

sqlserver.tf

add providers:

module "sqlServer" {
  source              = "./modules/sqlServers"
  deployPrivateLink   = true
  resource_group_name = "rg01"
  providers = {
    azurerm     = azurerm
    azurerm.dns = azurerm.dns
  }
}

main-sql.tf

add configuration_alias:

terraform {
  required_providers {
    azurerm = {
      source                = "hashicorp/azurerm"
      version               = "=3.79.0"
      configuration_aliases = [azurerm.dns]
    }
  }
}

and fix providers here:

module "privateLink_primary" {
  count               = var.deployPrivateLink ? 1 : 0
  source              = "../private_endpoints"
  resource_group_name = var.resource_group_name
  target_resource_id  = azurerm_mssql_server.primary.id
  providers = {
    azurerm     = azurerm
    azurerm.dns = azurerm.dns
  }
}

main-pe.tf

terraform {
  required_providers {
    azurerm = {
      source                = "hashicorp/azurerm"
      version               = "=3.79.0"
      configuration_aliases = [azurerm.dns]
    }
  }
}