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
- Add Missing Properties to AI Foundry Resource:
- Update your
azapi_resource.ai_foundrybody 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.
- Update your
- 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.
- Your assignments are partial; add missing perms for Foundry to Search:
- 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" }
- Update
- 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