To complement @jean-marie answer. You can have more information about referencing existing resources in your Bicep deployment with this Microsoft Q&A Video:
Type casting in bicep
I'm playing around with bicep and don't get how I can cast a param to i.e. Microsoft.Network/virtualNetworks
Example:
param vnet object
param name string
param addressPrefix string
param portfilter_mgmt_id string
resource Subnet 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' = {
parent: vnet
name: name
properties: {
addressPrefix: addressPrefix
networkSecurityGroup: {
id: portfilter_mgmt_id
}
}
}
vnet is typeof object
and must be casted to Microsoft.Network/virtualNetworks
somehow - Any idea?
3 answers
Sort by: Most helpful
-
-
SwathiDhanwada-MSFT 17,726 Reputation points
2022-01-06T12:21:19.257+00:00 @Tom Welcome to Microsoft Q & A Community Forum. From the example you have provided , seems you are trying to define child resource outside of the parent resource.
Below is sample example where you use the resource keyword to define a resource to deploy. Your resource declaration includes a symbolic name for the resource. You'll use this symbolic name in other parts of the Bicep file to get a value from the resource.
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = { name: 'virtualNetworkName' location: 'eastus' properties: { addressSpace: { addressPrefixes: [ '10.0.0.0/16' ] } } } resource subnet1Name 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' = { name: 'default' parent: vnet properties: { addressPrefix: '10.0.1.0/24' } }
-
Jean-Marie Geeraerts 1 Reputation point
2022-05-05T08:36:53.603+00:00 Hello @Tom , I'm making a few assumptions here, but if you are not able to as in the sample suggested by SwathiDhanwada-MSFT created both vnet and the child subnet at once, here's an alternative:
@description('Rather than using the vnet object, pass the vnet name and retrieve it via the vnet name') param vnetName string param name string param addressPrefix string param portfilter_mgmt_id string // Assuming that the vnet was created elsewhere, you can reference it by using the existing keyword resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' existing = { name: vnetName } resource subnet 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' = { parent: vnet // use the symbolic name of the vnet declaration above name: name properties: { addressPrefix: addressPrefix networkSecurityGroup: { id: portfilter_mgmt_id } } }