hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.53k stars 4.6k forks source link

azurerm_resource_group_template_deployment - API version information for RP "Microsoft.Insights" ("") was not found - activityLogAlerts #25458

Open cveld opened 5 months ago

cveld commented 5 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.7.1

AzureRM Provider Version

3.97.1

Affected Resource(s)/Data Source(s)

azurerm_resource_group_template_deployment

Terraform Configuration Files

main.tf:

data "local_file" "bicep_main" {
  filename = "${path.module}/main.bicep"
}
data "local_file" "bicep_nested" {
  filename = "${path.module}/nested.bicep"
}

resource "null_resource" "monitoring" {
  provisioner "local-exec" {
    command = "bicep build ${path.module}/main.bicep"
  }
  triggers = {
    bicep_main   = sha1(data.local_file.bicep_main.content)
    bicep_nested = sha1(data.local_file.bicep_nested.content)
  }
}

data "local_file" "monitoring" {
  depends_on = [null_resource.monitoring]
  filename   = "${path.module}/main.json"
}

resource "azurerm_resource_group_template_deployment" "monitoring" {
  name                = "test"
  resource_group_name = "rg-carlintveld"
  template_content    = data.local_file.monitoring.content
  deployment_mode     = "Incremental"
  parameters_content = jsonencode({
    "subscriptionId" = {
      value = data.azurerm_subscription.default.id
    }
  })
}

provider "azurerm" {
  features {}
}

data "azurerm_subscription" "default" {
  subscription_id = data.azurerm_client_config.default.subscription_id
}

data "azurerm_client_config" "default" {
}

main.bicep:
```bicep
param subscriptionId string

module activityLogAlert './nested.bicep' = {
  name: 'activityLogAlert'
  params: {
    subscriptionId: subscriptionId
  }
}

nested.bicep:

param subscriptionId string

resource activityLogAlerts_opt_devtst_postgresql_health_name_resource 'microsoft.insights/activityLogAlerts@2020-10-01' = {
  name: 'test'
  location: 'westeurope'
  properties: {
    enabled: false
    scopes: [
      subscriptionId
    ]
    condition: {
      allOf: [
        {
          field: 'category'
          equals: 'ResourceHealth'
        }
      ]}
  }
}

Debug Output/Panic Output

https://gist.github.com/cveld/281f7f785da48c47cb6c68235d7f9527

Expected Behaviour

The nested microsoft.insights/activityLogAlerts resource should have been deleted successfully.

Actual Behaviour

The provider errors and is not able to remove the nested resource:

╷
│ Error: removing items provisioned by this Template Deployment: API version information for RP "Microsoft.Insights" ("") was not found - nestedResource="/subscriptions/c3f6aacc-79ee-4bda-9ddc-29053356aa4f/resourceGroups/rg-carlintveld/providers/Microsoft.Insights/activityLogAlerts/test"
│ 

Steps to Reproduce

terraform apply -auto-approve terraform destroy -auto-approve

Important Factoids

No response

References

No response

teowa commented 5 months ago

Hi @cveld , thanks for submitting this! The API does not returns correct resource providers used if nested template(perform a deployment inside template_content) is used, as in https://github.com/Azure/azure-rest-api-specs/issues/28517, I have submitted PR https://github.com/hashicorp/terraform-provider-azurerm/pull/25466 to handle this.