Creating a new subscription and enabling a specific azure resource provider

Hello All. Parton my mess as this is my first post on this discussion board. Hoping someone can help me figure out what’s going on or what I am doing incorrectly.

Basically trying to “vend” instantiate a new Azure Subscription and then enable a specific azure resource provider that does not appear to get auto-enabled by the azurerm provider.

Here is an idea of what I am doing.

data “azurerm_billing_enrollment_account_scope” “ng_enrollment_billing_account_scope” {

billing_account_name    = local.billing_account_name

enrollment_account_name = local.enrollment_account_name

}

resource “azurerm_subscription” “subscription_vend” {

subscription_name = local.subscription_name

billing_scope_id  = data.azurerm_billing_enrollment_account_scope.ng_enrollment_billing_account_scope.id

#alias             = local.subscription_name

workload          = local.workload

tags              = local.required_tags

}

data “azurerm_subscription” “subscription_data” {

subscription_id = azurerm_subscription.subscription_vend.subscription_id

depends_on = [

   azurerm_subscription.subscription_vend

]

}

resource “azurerm_management_group_subscription_association” “subscription_to_mg” {

management_group_id = data.azurerm_management_group.management_group.id

subscription_id     = data.azurerm_subscription.subscription_data.id

depends_on = [

   azurerm_subscription.subscription_vend

]

}

resource “azurerm_resource_provider_registration” “ms_AlertsManagement” {

name = “Microsoft.AlertsManagement”

depends_on = [

   azurerm_subscription.subscription_vend

]

}

When Executed terraform will setup the new subscription, move it into the management group requested but then it fails when trying to enable the Microsoft.AlertsManagement azure resource provider.

I get the following error

Error: A resource with the ID “/subscriptions/redacted/providers/Microsoft.AlertsManagement” already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for “azurerm_resource_provider_registration” for more information.

│ with azurerm_resource_provider_registration.ms_AlertsManagement,
│ on main.tf line 58, in resource “azurerm_resource_provider_registration” “ms_AlertsManagement”:
│ 58: resource “azurerm_resource_provider_registration” “ms_AlertsManagement” {

│ A resource with the ID “/subscriptions/redacted/providers/Microsoft.AlertsManagement” already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for “azurerm_resource_provider_registration” for more information.

However if you check the subscription you will find that the AlertsManagement provider is not enabled.

Here is my providers.tf

terraform {

required_version = "~>0.15.1"

required_providers {

    azurerm = {

        version = "~>2.88.0"

        source  = "hashicorp/azurerm"

    }

    azuread = {

        version = "~>1.4.0"

        source  = "hashicorp/azuread"

    }        

}    

}

// Configure specific version of Azue Provider

provider “azurerm” {

environment     = local.environment

use_msi         = false

features {}

}

provider “azuread” {

environment     = local.environment

use_msi         = false

features {}

}