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.52k stars 4.6k forks source link

Getting unexpected status 412 with error: PreconditionFailed: Exception of type 'Microsoft.WindowsAzure.ApiManagement.Management.Core.Exceptions.PreconditionFailedException on running destroy job #24942

Open sushmitapandey opened 7 months ago

sushmitapandey commented 7 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.5.0

AzureRM Provider Version

3.90.0

Affected Resource(s)/Data Source(s)

azure apim product

Terraform Configuration Files

resource "azurerm_api_management_product" "this" {
  product_id            = var.apim_product_id
  resource_group_name   = var.resource_group_name
  api_management_name   = module.azure_prdsvc_terraform_apimanagement.name
  display_name          = var.apim_product_display_name
  description           = var.apim_product_description
  subscription_required = var.subscription_required
  subscriptions_limit   = var.subscriptions_limit
  approval_required     = var.approval_required
  published             = var.published
  depends_on            = [module.azure_prdsvc_terraform_apimanagement]
}

Debug Output/Panic Output

Error: deleting Product (Subscription: "12345678-1234-wygu-dshe-cc12738348273"
1452│ Resource Group Name: "demo-rg"
1453│ Service Name: "demo-apim-02"
1454│ Product: "product-api"): unexpected status 412 with error: PreconditionFailed: Exception of type 'Microsoft.WindowsAzure.ApiManagement.Management.Core.Exceptions.PreconditionFailedException' was thrown.

Expected Behaviour

It should destroy the apim and it's dependent resources

Actual Behaviour

Error: deleting Product (Subscription: "12345678-1234-wygu-dshe-cc12738348273" 1452│ Resource Group Name: "demo-rg" 1453│ Service Name: "demo-apim-02" 1454│ Product: "product-api"): unexpected status 412 with error: PreconditionFailed: Exception of type 'Microsoft.WindowsAzure.ApiManagement

After re-running the detroy job it didn't throw any error

Steps to Reproduce

create APIM module create apim product terraform apply terraform destroy will throw the exception re-run terraform destroy - job will get succeeded

Important Factoids

No response

References

No response

sinbai commented 6 months ago

Hi @sushmitapandey thanks for opening this issue. However, I could not reproduce this issue with the following TF configuration and steps. Could you please provide the minimal reproducible TF config (containing dependent resources and variable values) of this issue so that we could reproduce /troubleshoot?

Step1: Create a APIM service with the following config.


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

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "test" {
  name     = "acctestRG-0222"
  location = "eastus"
}

resource "azurerm_api_management" "test" {
  name                = "acctestAM-0222"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  publisher_name      = "pub1"
  publisher_email     = "pub1@email.com"

  sku_name = "Consumption_0"
}

Step2: Add the following config to create azurerm_api_management_product.

resource "azurerm_api_management_product" "test" {
  product_id            = "test-product"
  api_management_name   = azurerm_api_management.test.name
  resource_group_name   = azurerm_resource_group.test.name
  display_name          = "Test Product"
  subscription_required = true
  approval_required     = true
  published             = true
  subscriptions_limit   = 2
  description           = "This is an example description"
  terms                 = "These are some example terms and conditions"
}

Step3: Run Terraform destroy to destroy resources. image

sushmitapandey commented 6 months ago

This is an intermittent issue, it sometimes fails sometimes it runs fine. So wanted to check wat dependencies should I put for the product?