Accessing resource ids with `@nullIfNotFound()` cannot be done through `resourceSymbol.id` when using loops
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 79
Description
**Bicep version**
Bicep CLI version 0.41.2 (3e403ea7c1)
**Describe the bug**
While testing the `nullIfNotFound` feature to validate the existence of subnets before adding the to resource firewalls from storage accounts, keyvaults, ... we created a module doing the following:
1. Take a resource id of the target subnet as parameter
2. Retrieve the resource group name, subnet name and vnet name from the string
3. Get the vnet then subnet resource annotated with the `nullIfNotFound` decorator
4. Retrieve the id of the subnet IDs of the vnets that do exist
While we did not encounter any compile time error, when we ran the module in Azure, we encountered an issue where accessing the subnet resource ID through the property `id` always returned null, despite the subnet being found in the deployment UI.
After returning the whole object for debugging, we noticed that the `id` property did not exist at all and instead a `resourceId` property was present in the response. Furthermore, this resource ID is not the full id but the id starting from the resource provider.
**To Reproduce**
Run the following bicep file:
```bicep
@description('Subnet resource ID to validate')
param subnetId string
func parseSubnetResourceId(subnetResourceId string) object => {
subnetName: split(subnetResourceId, '/subnets/')[1]
vnetName: last(split(split(subnetResourceId, '/subnets/')[0], '/'))
resourceGroup: split(split(subnetResourceId, '/resourceGroups/')[1], '/')[0]
}
var parsedSubnet = parseSubnetResourceId(subnetId)
@nullIfNotFound()
resource vnetResource 'Microsoft.Network/virtualNetworks@2025-05-01' existing = {
name: parsedSubnet.vnetName
scope: resourceGroup(parsedSubnet.resourceGroup)
}
@nullIfNotFound()
resource snetResource 'Microsoft.Network/virtualNetworks/subnets@2025-05-01' existing = {
name: parsedSubnet.subnetName
parent: vnetResource
}
@description('Existing subnet resource ID, or null when subnet is not found')
output existingSubnet object? = snetResource
```
The resource id is broken in 3 different properties and has to be built manually:
- resourceGroupName
- resourceId
- subscriptionId
**Additional context**
See https://github.com/Azure/bicep/issues/19553#issuecomment-4394698675
Contributor guide
Assessment
This issue has not been assessed yet.