UDF creating a resourceid for subnets #18095
Replies: 2 comments
-
Something is not quite right. The loop for var Something is missing... |
Beta Was this translation helpful? Give feedback.
-
Hey folks — you don't need anything fancy here. Subnets are children of vNets, so the resourceId shape includes both names. In Bicep you can keep it simple with a tiny helper: func subnetId(vnetName string, subnetName string) string => resourceId('Microsoft.Network/virtualNetworks/subnets', vnetName, subnetName) Usage: If you're building cross-scope templates (different RG/sub), use the long form: That's worked reliably for me across deployments. If you're still hitting errors, share the exact expression and I'll help spot the typo. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I created a UDF
@export()
@description('Custom type for referencing a subnet')
type subnetType = {
@description('The name of the subnet')
subnet: string
@description('The name of the virtual network which contains the subnet')
virtualNetwork: string
@description('The resource group containing the vnet if different than that of the resource being deployed')
resourceGroup: string?
@description('The ID of the subscription containing the vnet if different than that of the resource being deployed')
subscriptionId: string?
}
@export()
func subnetId(subnet subnetType) string => resourceId(subnet.?subscriptionId ?? subscription().subscriptionId, subnet.?resourceGroup ?? resourceGroup().name,'Microsoft.Network/VirtualNetworks/subnets', subnet.virtualNetwork, subnet.subnet)
I have been using successfully to create a resourceid for virtual networks rules. The issue I am having is trying to loop to add multiple subnetId to virtual networks. rules.
here is the output for a single resourceId:
"outputs": {
"subtest": {
"type": "String",
"value": "/subscriptions/ed37781a-e955-44dc-8ffc-9ea2ecae1231/resourceGroups/resourceGroupName/providers/Microsoft.Network/VirtualNetworks/vnetName/subnets/subnetName"
here is the output for multiple resourceIds
vivirtualNetworkRulesVar": {
"type": "Array",
"value": [
{
"id": "/subscriptions/ed37781a-e955-44dc-8ffc-9ea2ecae1231/resourceGroups/resourceGroupName/providers/Microsoft.Network/VirtualNetworks/vnetName/subnets/subnetName",
"resourceGroup": "resourceGroupName"
},
{
"id": "/subscriptions/ed37781a-e955-44dc-8ffc-9ea2ecae1231/resourceGroups/resourceGroupName/providers/Microsoft.Network/VirtualNetworks/vnetName/subnets/subnetName",
"resourceGroup": "resourceGroupName"
}
]
}
for some reason looping the function is adding "resourceGroup": "resourceGroupName"
this is my test file
import * as subnetmodule from 'br:crshrdtfa01.azurecr.io/bicep/modules/type/type-subnet:1.0-latest'
type virtualNetworkRulesType = {
id: subnetmodule.subnetType
ignoreMissingVNetServiceEndpoint: bool
}
param virtualNetworkRules array = [
{
id: {
subnet: 'subnet1'
virtualNetwork: 'vnet'
resourceGroup: 'resourcegroupName'
}
ignoreMissingVNetServiceEndpoint: false
}
{
id: {
subnet: 'subnet2'
virtualNetwork: 'vnet'
resourceGroup: 'rsourcegroupName'
}
ignoreMissingVNetServiceEndpoint: true
}
]
param test subnetmodule.subnetType = {
subnet: 'subnet3'
virtualNetwork: 'vnet'
resourceGroup: 'resourcegroupName'
}
var sub= subnetmodule.subnetId(test)
var virtualNetworkRulesVar = [
for item in virtualNetworkRules : {
ignoreMissingVNetServiceEndpoint: item.ignoreMissingVNetServiceEndpoint
ids:subnetmodule.subnetId(item.id)
}
]
output vivirtualNetworkRulesVar array = virtualNetworkRulesVar
output subtest string= sub
Any thoughts on why this is happening?
Patti
Beta Was this translation helpful? Give feedback.
All reactions