Azure Bicep Networking Examples

· 2 min read

done AzureBicepNetworking

Virtual Network and Subnets Here i demonstrate how to create a virtual network and subnet.


// 1. declaring the scope of the bicep file

targetScope =  'resourceGroup'



//2. Declareing the variables

var location = resourceGroup().location



//3. deploying the modules

//refrencing the Server NSG

module nsg_server 'nsg_server.bicep' ={

  params:{location:location}

}


//5. Declaring the resources
// Declaring the Virtual Network where subnet sit inside

resource vnet 'Microsoft.Network/virtualNetworks@2023-09-01'={

  name:'Vnet_soclab'

  location: location

  properties:{

    addressSpace:{addressPrefixes:['10.50.0.0/16']}
 }

}



//Declaring subnets as their own resources

// Subnet server

resource subnetServer 'Microsoft.Network/virtualNetworks/subnets@2023-09-01' = {

  parent: vnet

  name: 'Subnet_server'

  properties:{addressPrefix:'10.50.1.0/24'

        networkSecurityGroup:{id:nsg_server.outputs.nsgServerId}}

}

}

Network Security Group (NSG)

Here I demonstrate how to create a Network security Group and add rules.

// Declaring the the resource
resource nsg 'Microsoft.Network/networkSecurityGroups@2024-01-01' = {
  name: 'nsg-web-001'
  location: location
  properties: {
    securityRules: [
    // creating the security rules 
      {
        name: 'Allow-HTTPS-Inbound'
        properties: {
          priority: 100
          direction: 'Inbound'
          access: 'Allow'
          protocol: 'Tcp'
          sourcePortRange: '*'
          destinationPortRange: '443'
          sourceAddressPrefix: 'Internet'
          destinationAddressPrefix: '10.0.1.0/24'
          description: 'Allow HTTPS from internet'
        }
      }
      {
        name: 'Deny-All-Inbound'
        properties: {
          priority: 4096
          direction: 'Inbound'
          access: 'Deny'
          protocol: '*'
          sourcePortRange: '*'
          destinationPortRange: '*'
          sourceAddressPrefix: '*'
          destinationAddressPrefix: '*'
        }
      }
    ]
  }
}

// you can either associate the NSG here to network or in the network file where you define the network.


Public IP and Network Interface

Here is an example of how declare a public IP and network interface in a bicep file

// Declaring the resoruce 
resource publicIp 'Microsoft.Network/publicIPAddresses@2024-01-01' = {
  name: 'pip-vm-web-001'
  location: location
  // sku refers to the tier of the service 
  sku: {
    name: 'Standard'
    zone: '1,2,3'
  }
  properties: {
    publicIPAllocationMethod: 'Static'
    publicIPAddressVersion: 'IPv4'
  }
}
// Decalring the network interface card 
resource nic 'Microsoft.Network/networkInterfaces@2024-01-01' = {
  name: 'nic-vm-web-001'
  location: location
  properties: {
    ipConfigurations: [
      {
      //associating the nic to a Vnet
        name: 'ipconfig1'
        properties: {
          subnet: {
            id: vnet.properties.subnets[0].id
          }
          // associating the nic to a public ip 
          publicIPAddress: {
            id: publicIp.id
          }
          privateIPAllocationMethod: 'Dynamic'
        }
      }
    ]
    // associating the nic to a network security group
    networkSecurityGroup: {
      id: nsg.id
    }
  }
}