Cannot create VNET, the resource group is missing. StatusCode=404

Hello!

I have problems to create the virtual network. I am getting the same error when:

  1. The resource group already exist
  2. The resource group is created in the same “apply”

And here is the smallest config I have to show the problem:

terraform {
required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “2.85.0”
}
}
}
provider “azurerm” {
features {}
}

locals {
azure_location = “West Europe”
}

provider “azurerm” {
alias = “hub-sub”
subscription_id = “4c19dd9e-b3c3-4ae2-a639-4dd4373a843c”
tenant_id = “91f09566-a850-4fae-bbe1-29ad3804a2f6”
features {}
}

resource “azurerm_resource_group” “dev_rg” {
provider = azurerm.hub-sub
name = “dev_rg”
location = local.azure_location
}

resource “azurerm_virtual_network” “vnet-hub-we” {
name = “vnet-hub-we”
location = local.azure_location
resource_group_name = “azurerm_resource_group.dev_rg.name”
address_space = [“10.0.0.0/16”]
dns_servers = [“10.0.0.4”, “10.0.0.5”]

subnet {
name = “subnet1”
address_prefix = “10.0.1.0/24”
}
}

This is the error message:

C:\Users\jais\Azure\PN-Infra>terraform apply “terraform-plan.tfplan”
azurerm_virtual_network.vnet-hub-we: Creating…

Error: creating/updating Virtual Network: (Name “vnet-hub-we” / Resource Group “azurerm_resource_group.dev_rg.name”): network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=404 – Original Error: Code=“ResourceGroupNotFound” Message=“Resource group ‘azurerm_resource_group.dev_rg.name’ could not be found.”

on provider.tf line 30, in resource “azurerm_virtual_network” “vnet-hub-we”:
30: resource “azurerm_virtual_network” “vnet-hub-we” {

C:\Users\jais\Azure\PN-Infra>

I found the problem. We are using several subscriptions i.e. having a provider for each. Even though the resource group was created with a certain provider, also the network resource was forced to have the following statement:

provider = azurerm.provider-sub-hub-we