question

Tom-7577 avatar image
0 Votes"
Tom-7577 asked JeanMarieGeeraerts-1800 answered

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?





azure-dtl-arm-enviorments
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SwathiDhanwada-MSFT avatar image
0 Votes"
SwathiDhanwada-MSFT answered

@Tom-7577 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'
    
 }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JeanMarieGeeraerts-1800 avatar image
0 Votes"
JeanMarieGeeraerts-1800 answered

Hello @Tom-7577, 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
     }
   }
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.