Type casting in bicep

Tom 1 Reputation point
2022-01-01T15:54:11.69+00:00

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 DevTest Labs
Azure DevTest Labs
An Azure service that is used for provisioning development and test environments.
256 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Stephane Lapointe 6 Reputation points MVP
    2023-01-31T23:32:43.61+00:00

    To complement @jean-marie answer. You can have more information about referencing existing resources in your Bicep deployment with this Microsoft Q&A Video:

    https://www.youtube.com/watch?v=H1g_pj1uo5E

    1 person found this answer helpful.
    0 comments No comments

  2. 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'  
      
    }  
    }  
    
    0 comments No comments

  3. 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  
        }  
      }  
    }  
    
    0 comments No comments