Azure / Azure/bicep-types-az

[<Microsoft.ApiManagement/service/apis/policies@2024-06-01-preview>]: <The template resource APIM Policies has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name>

Open
#2,425 0 comments 0 reactions 0 assignees View on GitHub
inaccuracy
Dominant language
TypeScript
Stars
108
Forks
44
Avg merge
18h 53m
Merged PRs (30d)
29

Description

### Resource Type

Microsoft.ApiManagement/service

### Api Version

2024-06-01-preview

### Issue Type

Resource fails to deploy

### Other Notes

I am deploying an existing APIM instance using Bicep template but creating a new Product for a client and also referencing function key (default host key) in APIM bicep. I am using YAML pipeline to deploy APIM bicep but pipeline is failing with following error which I am unable to resolve:

```
There were errors in your deployment. Error code: InvalidTemplate.
##[error]Deployment template validation failed: 'The template resource '/MasterData-2025-02-04/policy' for type 'Microsoft.ApiManagement/service/apis/policies' at line '1' and column '2229' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-syntax-resources for usage details.'.
##[warning]Validation errors were found in the Azure Resource Manager template. This can potentially cause template deployment to fail. Task failed while creating or updating the template deployment.. Please follow https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-syntax
Starting Deployment.
Deployment name is APIM-20250204-071435-c1cc
There were errors in your deployment. Error code: InvalidTemplate.
##[error]Deployment template validation failed: 'The template resource '/MasterData-2025-02-04/policy' for type 'Microsoft.ApiManagement/service/apis/policies' at line '1' and column '2229' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-syntax-resources for usage details.'.
##[error]Check out the troubleshooting guide to see if your issue is addressed: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment?view=azure-devops#troubleshooting
##[error]Task failed while creating or updating the template deployment.
```
I posted this issue in Developer Community for Azure DevOps which can be checked from following link:
https://developercommunity.visualstudio.com/t/The-template-resource-APIM-Policies-has-/10841465
The engineering team from the Developer Community asked me to report this issue here.

### Bicep Repro

@description('Enter name of Integration')
param IntegrationName string

@description('Enter location for your resources')
@allowed([
'westeurope'
])
param Location string

param StorageAccountType string

//APIM
param ApimName string
param AppServiceUrl string
param AppServiceSwaggerUrl string
param ApiVersion string
param AiName string
param ApiName string

//Function App Configurations
param EnvironmentLevel string
param ApplicationName string

@description('Enter Enviroment of the resource')
@allowed([
'Dev'
'Prod'
])
param Environment string

//KeyVault
param KeyVaultName string
param KeyVaultRGName string

//Service Plan Sku
param SpSkuName string
param SpSkuTier string

var apiPath = '${toLower(ApiName)}'
var apiDisplayName = '${ApiName}-${ApiVersion}'

resource apim 'Microsoft.ApiManagement/service@2024-06-01-preview' existing = {
name: ApimName

// Api Version Set
resource apiVersionSet 'apiVersionSets@2024-06-01-preview' = {
name: ApiName
properties: {
displayName: ApiName
versioningScheme: 'Header'
versionHeaderName: 'X-Matas-Api-Version'
}
}

// Api Collection
resource apiResource 'apis@2024-06-01-preview' = {
name: apiDisplayName

properties: {
displayName: apiDisplayName
serviceUrl: AppServiceUrl
path: apiPath
format: 'openapi+json-link'
value: AppServiceSwaggerUrl
apiVersion: ApiVersion
protocols: [
'https'
]
subscriptionRequired: true
subscriptionKeyParameterNames: {
header: 'X-Matas-Subscription-Key'
}
apiVersionSetId: apiVersionSet.id
}

// Validation Policy
resource validationPolicy 'policies' = {
name: 'policy'
dependsOn: [
apiResource
]
properties: {
value: loadTextContent('allOperationsPolicy.xml')
format: 'rawxml'
}
}

// Diagnostics
resource apiDiagnostics 'diagnostics' = {
name: 'applicationinsights'
properties: {
loggerId: resourceId('Microsoft.ApiManagement/service/loggers', ApimName, AiName)
logClientIp: false
alwaysLog: 'allErrors'
sampling: {
samplingType: 'fixed'
percentage: 100
}
}
}
}

// Assigning Products to API
resource inriverDatahub 'products@2024-06-01-preview' = {
name: 'InriverDatahub'
properties: {
displayName: 'InriverDatahub'
subscriptionRequired: true
state: 'published'
}
}
}

// Named values
resource funcKey 'Microsoft.ApiManagement/service/namedValues@2024-06-01-preview' = {
parent: apim
name: 'func-masterdataintegration-weu-dev-key'
properties: {
displayName: 'func-masterdataintegration-weu-dev-key'
// keyVault: {
// identityClientId: 'string'
// secretIdentifier: 'string'
// }
// secret: bool
// tags: [
// 'string'
// ]
value: appKeys.outputs.defaultHostKey
}
}

module appKeys '../func.bicep' = {
name: 'App keys'
params: {
ApplicationName: ApplicationName
Environment: Environment
EnvironmentLevel: EnvironmentLevel
IntegrationName: IntegrationName
KeyVaultName: KeyVaultName
KeyVaultRGName: KeyVaultRGName
Location: Location
SpSkuName: SpSkuName
SpSkuTier: SpSkuTier
StorageAccountType: StorageAccountType
}
}

[IaC.zip](https://github.com/user-attachments/files/18657782/IaC.zip)

### Confirm

- [x] I have read the troubleshooting guide and looked for duplicates.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the Bicep repro, especially the validationPolicy declaration nested under apiResource, and inspect the attached IaC.zip and allOperationsPolicy.xml for context. Compare the generated APIM policy resource path and type segments, then validate the YAML deployment; done means the template validates and deploys without the reported InvalidTemplate error.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure
Domain
api, cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.