G_S
G_S

Reputation: 105

409 conflict while updating multiple properties of terraform azapi_resource

We are using terraform's az_api resource to create Azure openai content filter(rai_policy). While updating the contentFilters property for multiple resources , we are seeing below error

**RESPONSE 409: 409 Conflict
│ ERROR CODE: RequestConflict
│ --------------------------------------------------------------------------------
│ {
│   "error": {
│     "code": "RequestConflict",
│     "message": "Another operation is being performed on the parent resource. Please try again later."     
│   }
│ }**

Can someone help how this conflict can be resolved?

Upvotes: 0

Views: 34

Answers (1)

Vinay B
Vinay B

Reputation: 2401

Updating the contentFilters property for multiple resources deployments

Issue may be due to dependency issue while concurrent operations, where one resource provision may depend on the other or while the other resource is yet to provision or busy in deployment of other operation.

The requirement you are looking for still possible to achieve using sequel deployment, i.e., one after the other.

Demo configuration:

resource "azurerm_cognitive_account" "cognitive_account" {
  name                = "vinay-cas"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  kind                = "OpenAI"
  sku_name            = "S0"

  depends_on = [azurerm_resource_group.rg]
}

locals {
  content_filters = [
    { name = "Hate", filter_enabled = true, block_enabled = true, severity_threshold = "High", source = "Prompt" },
    { name = "Sexual", filter_enabled = true, block_enabled = true, severity_threshold = "High", source = "Prompt" },
    { name = "Violence", filter_enabled = true, block_enabled = true, severity_threshold = "High", source = "Prompt" },
    { name = "SelfHarm", filter_enabled = true, block_enabled = true, severity_threshold = "High", source = "Prompt" }
  ]
}

resource "azurerm_cognitive_account_rai_policy" "example" {
  name                 = "vinay-rai-policy"
  cognitive_account_id = azurerm_cognitive_account.cognitive_account.id
  base_policy_name     = "Microsoft.Default"

  dynamic "content_filter" {
    for_each = local.content_filters
    content {
      name               = content_filter.value.name
      filter_enabled     = content_filter.value.filter_enabled
      block_enabled      = content_filter.value.block_enabled
      severity_threshold = content_filter.value.severity_threshold
      source             = content_filter.value.source
    }
  }

  depends_on = [azurerm_cognitive_account.cognitive_account]
}

locals {
  deployments = ["vinay-cd1", "vinay-cd2"]
}

resource "azurerm_cognitive_deployment" "deployments" {
  for_each              = toset(local.deployments)
  name                 = each.value
  cognitive_account_id = azurerm_cognitive_account.cognitive_account.id
  rai_policy_name      = azurerm_cognitive_account_rai_policy.example.name

  model {
    format  = "OpenAI"
    name    = "gpt-4o"
    version = "2024-05-13"
  }

  sku {
    name     = "GlobalStandard"
    capacity = 1
  }

  depends_on = [azurerm_cognitive_account.cognitive_account]
}

Deployment:

enter image description here

enter image description here

enter image description here

refer:

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cognitive_deployment#rai_policy_name-1

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cognitive_account_rai_policy

Terraform 409 Conflict Errors: Understand & Resolve

Upvotes: 0

Related Questions