Existing resource names must be unique in language version 2.0
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Bicep version**
Bicep CLI version 0.25.53 (c0ad57dff6)
**Describe the bug**
I have stumbled upon the following deployment error:
```
New-AzDeployment: 14:50:09 - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The resource 'Microsoft.Authorization/policyDefinitions/96670d01-0a4d-4649-9c89-2d3abc0a5025' at line '8' and column '36' is defined multiple times in a template. Please see https://aka.ms/arm-syntax-resources for usage details.'.
New-AzDeployment: The deployment validation failed
```
I am trying to do two different policy assignments for the same policy definition. Interestingly the error appears only if there is schema (user defined types) defined and runs without issue if I remove the user defined types from code. The issue is also not present even with schema when the referenced policy definitions do not repeat.
**To Reproduce**
Steps to reproduce the behavior:
input-types.bicep
```bicep
@export()
type policyAssignment = {
@description('''The ID of the policy assignment.''')
id: string
@description('Display name for the policy assignment.')
@maxLength(128)
name: string
@description('''The ID of the policy definition.''')
policyDefinitionId: string
@description('Pair of name and value to be added.')
metadata: metadata?
@description('''The policy definition parameters and values''')
parameters: parametersPolicyDefinition?
}
type parametersPolicyDefinition = {
@description('''The name of the parameter.''')
*: {}
}
type metadata = {
@description('The value for the metadata name.')
*: string
}
@export()
type tagsType = {
@description('''The tag name and value pair.''')
*: string
}
@description('Deploys Policy Assignments for policy definitions.')
param policyAssignments policyAssignment[] = []
```
main.bicep
```bicep
targetScope = 'subscription'
import * as inputSchema from 'input-types.bicep'
param policyAssignments inputSchema.policyAssignment[] = [
{
id: 'require-tag-one'
name: 'Require a tag on resource groups: [tag-one]'
policyDefinitionId: '96670d01-0a4d-4649-9c89-2d3abc0a5025'
parameters: {
tagName: {
value: 'tag-one'
}
}
}
{
id: 'require-tag-two'
name: 'Require a tag on resource groups: [tag-two]'
policyDefinitionId: '96670d01-0a4d-4649-9c89-2d3abc0a5025'
parameters: {
tagName: {
value: 'tag-two'
}
}
}
]
resource tenantPolicyDefinitions 'Microsoft.Authorization/policyDefinitions@2023-04-01' existing = [for (policyAssignment, i) in policyAssignments: {
name: policyAssignment.policyDefinitionId
scope: tenant()
}]
resource policyAssignmentsRes 'Microsoft.Authorization/policyAssignments@2023-04-01' = [for (policyAssignment, i) in policyAssignments: {
name: policyAssignment.id
location: deployment().location
properties: {
displayName: policyAssignment.name
policyDefinitionId: tenantPolicyDefinitions[i].id
metadata: {}
parameters: policyAssignment.parameters
}
}]
```
**Additional context**
Add any other context about the problem here.
Contributor guide
Assessment
This issue has not been assessed yet.