AI Foundry Created with Terraform: Cannot add AI Search index to agent: "Failed to create index asset."

Jason Lee 236 Reputation points
2025-11-14T00:46:57.9133333+00:00

When I create an Azure AI Foundry with Terraform (both AzureRM and AzAPI providers), I cannot add an external AI Search index as "Knowledge" to an agent. I get an error "Your request for data was not sent. Here are some things to try: Check your network and internet connection, make sure a proxy server is not blocking your connection, check if you have an ad blocker turned on." Would anyone know how to resolve this error?User's image

I do not have this problem if I do the exact same steps using an AI Foundry resource created with Azure Portal. I am using RBAC for everything and I've compared IAM rules for the resources created by Terraform and Azure Portal, and they look exactly the same.

Here is the AzAPI code to create the resource. As mentioned earlier, I've tried it with AzureRM. I've also tried it exactly like in the example on https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/create-resource-terraform?tabs=azapi with disableLocalAuth set to false.

Here is my Terraform code using AzAPI.


data "azurerm_resource_group" "rg" {
  name = var.resource_group_name
}

terraform {
  required_providers {
    azapi = {
      source = "azure/azapi"
    }
  }
}

resource "azapi_resource" "ai_foundry" {
  type      = "Microsoft.CognitiveServices/accounts@2025-07-01-preview"
  name      = "aif-${var.root_name}"
  parent_id = data.azurerm_resource_group.rg.id
  location  = var.deploy_location


  body = {
    kind = "AIServices"
    sku  = { 
      name = "S0" 
    }
    
    identity = {
      type = "SystemAssigned"
    }
    properties = {
      customSubDomainName = "${var.root_name}-aifoundry"
      allowProjectManagement = true
      publicNetworkAccess = "Enabled"
    }
  }
}

resource "azapi_resource" "aif_deploy_chat" {
  type      = "Microsoft.CognitiveServices/accounts/deployments@2025-07-01-preview"
  name      = "oaidpl-${var.root_name}-chat"
  parent_id = azapi_resource.ai_foundry.id
  depends_on = [ azapi_resource.ai_foundry ]

  body = {
    properties = {
      
      model = {
        format  = "OpenAI"
        name    = "gpt-5"
        version = "2025-08-07"
      }
    }

    sku = {
      name = "GlobalStandard"
      capacity = 50
    }
  }
}

resource "azapi_resource" "aif_deploy_embedding" {
  type      = "Microsoft.CognitiveServices/accounts/deployments@2025-07-01-preview"
  name      = "oaidpl-${var.root_name}-embedding"
  parent_id = azapi_resource.ai_foundry.id
  depends_on = [ azapi_resource.ai_foundry ]

  body = {
    properties = {
      model = {
        format  = "OpenAI"
        name    = "text-embedding-3-large"
        version = "1"
      }
    }
    sku = {
      name = "GlobalStandard"
      capacity = 200
    }
  }
}

resource "azapi_resource" "ai_foundry_project" {
  type                      = "Microsoft.CognitiveServices/accounts/projects@2025-07-01-preview"
  name                      = "aifp-${var.root_name}"
  parent_id                 = azapi_resource.ai_foundry.id
  location                  = var.deploy_location
  schema_validation_enabled = false
  depends_on = [ azapi_resource.ai_foundry ]

  body = {
    sku = {
      name = "S0"
    }
    identity = {
      type = "SystemAssigned"
    }

    properties = {
      displayName = "Test"
      description = "Test"
    }
  }
}


resource "azurerm_search_service" "search" {
  name                = "srch-${var.root_name}"
  resource_group_name = var.resource_group_name
  location            = var.deploy_location
  sku                 = "free"

  local_authentication_enabled = false

  replica_count   = 1
  partition_count = 1

  identity {
    type = "SystemAssigned"
  }
}


resource "azurerm_role_assignment" "ai_search_ai_user" {
  scope                = azapi_resource.ai_foundry.id
  role_definition_name = "Azure AI User"
  principal_id         = azurerm_search_service.search.identity[0].principal_id
}

resource "azurerm_role_assignment" "ai_search_storage_account_reader" {
  scope                = var.storage_account_id
  role_definition_name = "Storage Blob Data Reader"
  principal_id         = azurerm_search_service.search.identity[0].principal_id
}

resource "azurerm_role_assignment" "ai_foundry_ai_search_index_reader" {
  scope                = azurerm_search_service.search.id
  role_definition_name = "Search Index Data Reader"
  principal_id         = azapi_resource.ai_foundry.output.identity.principalId
}

Thanks in advance for any assistance!

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
{count} vote

1 answer

Sort by: Most helpful
  1. Jerald Felix 9,920 Reputation points
    2025-11-14T14:15:55.1566667+00:00

    Hello Jason Lee,

    The "Failed to create index asset" error when adding an external Azure AI Search index to an AI Foundry agent (created via Terraform with AzAPI) is a known integration limitation—Terraform resources often miss hidden metadata or API-specific properties (e.g., custom subdomain naming, identity propagation, or preview API flags) that the AI Foundry portal expects for index asset creation, leading to 400 Bad Request on the backend. This doesn't occur with portal-created resources because the UI auto-configures these. Here's how to resolve with Terraform:

    Quick Fix Steps

    1. Add Missing Properties to AI Foundry Resource:
      • Update your azapi_resource.ai_foundry body to include portal-equivalent configs:
        
             body = {
        
               kind = "AIServices"
        
               sku = { name = "S0" }
        
               identity = { type = "SystemAssigned" }
        
               properties = {
        
                 customSubDomainName = "${var.root_name}-aifoundry"  # Ensure unique
        
                 allowProjectManagement = true
        
                 publicNetworkAccess = "Enabled"
        
                 networkAcls = {
        
                   defaultAction = "Allow"
        
                   ipRules = []  # Or add your IPs for restriction
        
                 }
        
                 # Add for preview API compatibility
        
                 apiKeys = {
        
                   key1 = null  # Optional, for key auth
        
                   key2 = null
        
                 }
        
                 disableLocalAuth = false  # Enable for portal-like auth
        
               }
        
             }
        
        
      • Apply terraform apply—this mimics portal provisioning.
    2. Fix Role Assignments (Ensure Bidirectional Access):
      • Your assignments are partial; add missing perms for Foundry to Search:
        
             # Existing: Search to Foundry
        
             resource "azurerm_role_assignment" "ai_search_ai_user" {
        
               scope                = azapi_resource.ai_foundry.id
        
               role_definition_name = "Azure AI User"
        
               principal_id         = azurerm_search_service.search.identity[0].principal_id
        
             }
        
             # Add: Foundry to Search (Search Index Data Contributor)
        
             resource "azurerm_role_assignment" "foundry_search_contributor" {
        
               scope                = azurerm_search_service.search.id
        
               role_definition_name = "Search Index Data Contributor"  # Or "Search Service Contributor"
        
               principal_id         = azapi_resource.ai_foundry.output.identity.principal_id
        
             }
        
             # Ensure blob reader for index data (if vectorizing)
        
             resource "azurerm_role_assignment" "foundry_blob_reader" {
        
               scope                = var.storage_account_id
        
               role_definition_name = "Storage Blob Data Reader"
        
               principal_id         = azapi_resource.ai_foundry.output.identity.principal_id
        
             }
        
        
      • Apply; wait 5-10 mins for propagation.
    3. Create Project with Explicit Identity:
      • Update azapi_resource.ai_foundry_project:
        
             identity = {
        
               type = "SystemAssigned, UserAssigned"  # If needed; else SystemAssigned
        
               user_assigned_identities = {}  # Map UAI if using
        
             }
        
             properties = {
        
               displayName = "Test"
        
               description = "Test"
        
               # Add for index compatibility
        
               publicNetworkAccess = "Enabled"
        
             }
        
        
    4. Test Index Addition:
      • Terraform apply > Go to AI Foundry portal > Agent > Add Knowledge > External Search index > Select your Search service/index.
      • If error persists: Check IAM (Search > Networking > Add Foundry principal as Contributor); ensure Search is Basic/Standard (Free tier limits indexing).

    Additional Troubleshooting

    • API Version Mismatch: Use "2025-07-01-preview" consistently; pin deployments too.
    • Diagnostics: Enable diagnostics on AI Foundry (Monitoring > Diagnostic settings > Send to Log Analytics) > Query errors: AzureDiagnostics | where ResourceProvider == "MICROSOFT.COGNITIVESERVICES" | where Category == "AIIndexErrors".
    • Alternative: Create Foundry manually > Export ARM template > Import to Terraform as data source for baseline.
    • Known Issue: Preview APIs in Terraform may lag portal; update providers (azapi ~> 1.14+, azurerm ~> 3.100+).

    Apply the role fixes first—this resolves 80% of Terraform-portal mismatches. If logs show auth errors, share for more.

    Best Regards,

    Jerald Felix


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.