APIM ARM部署脚本报错:无法找到指定backendId的问题排查求助
Let's break down exactly what's going on here and how to fix it, plus get your dynamic configuration working properly:
The Root Cause
That validation error is telling you the value you're using in your set-backend-service policy doesn't match the name of any backend resource in your APIM instance.
You're right it relates to the Microsoft.ApiManagement/service/backends resource, but here's the key detail: the "backend id" referenced in the policy isn't a separate id property on the backend resource—it's the resource name of the backend. The description field is just a human-readable note and has no bearing on policy references, which is why modifying it didn't break anything (but also didn't fix your error).
How to Get the Correct Backend Name
First, confirm what your backend's actual name is:
- In your ARM template, look at the
nameproperty of yourMicrosoft.ApiManagement/service/backendsresource. It's usually formatted as[concat(parameters('apimServiceName'), '/<backend-name>')]—the part after the slash is the name you need to use in the policy. - If working with an existing APIM instance, check the Azure Portal: go to your APIM service > Backends, select the relevant backend, and look at the Name field (not the display name).
- Or use Azure CLI to fetch it directly:
Theaz apim backend show --resource-group <your-resource-group> --service-name <your-apim-name> --backend-name <your-backend-display-name>nameproperty in the output is the value you need.
Implementing Dynamic Configuration
To avoid hardcoding and fix the error, link your policy's backend reference directly to your backend resource's name using ARM template parameters/variables:
Define a parameter for your backend name (or use a variable if it's derived):
"parameters": { "apimServiceName": { "type": "string", "metadata": { "description": "Name of the APIM service" } }, "backendFunctionAppName": { "type": "string", "defaultValue": "tostore-functionapp", "metadata": { "description": "Name of the backend Function App" } } }Reference this parameter in your backend resource definition:
{ "type": "Microsoft.ApiManagement/service/backends", "apiVersion": "2023-03-01-preview", "name": "[concat(parameters('apimServiceName'), '/', parameters('backendFunctionAppName'))]", "dependsOn": [ "[resourceId('Microsoft.ApiManagement/service', parameters('apimServiceName'))]" ], "properties": { "description": "Backend for tostore Function App", // This can be anything, it's just a note "url": "[concat('https://', parameters('backendFunctionAppName'), '.azurewebsites.net/api')]", // Other backend properties (like authentication) } }Use the same parameter in your
set-backend-servicepolicy:<policies> <inbound> <base /> <!-- Correct: uses the dynamic backend name parameter --> <set-backend-service backend-id="[parameters('backendFunctionAppName')]" /> </inbound> <!-- Rest of your policies --> </policies>
Why Your Previous "Fix" Worked (But Didn't Solve the Root Issue)
When you modified the description field, you weren't changing anything the policy uses to locate the backend. The only reason your deployment might have worked after that is if you accidentally had a backend resource with a name matching tostore-functionapp already present in your APIM instance.
内容的提问来源于stack exchange,提问作者Daarwin

