Azure VM Deployment Using Bicep!

What is Bicep in Azure?

Bicep is an open-source, domain-specific language (DSL) developed by Microsoft for deploying Azure resources using Infrastructure as Code (IaC) principles. Bicep aims to simplify defining, deploying, and managing Azure resources by providing a cleaner and more concise syntax. It is designed to be a more user-friendly and readable alternative to Azure Resource Manager (ARM) template written in JSON.

Key features of the Bicep

  1. Readability- Bicep's syntax is more human-readable and less verbose than ARM templates, making it easier to understand and write the infrastructure code.
  2. Strong typing- Bicep provides type safety and validation, which helps catch errors during development and improves code completion and parameter validation.
  3. Modularity- Bicep allows you to create reusable modules that can be shared across different projects, enabling better code organization and simplifying complex deployments.
  4. Tooling support- Bicep is supported by various tools and editors, such as Visual Studio Code, which offers a Bicep extension for syntax highlighting, autocompletion, and validation.
  5. Interoperability with ARM templates- Bicep files can be compiled into ARM templates, enabling a smooth transition for organizations that already use ARM templates for their Azure deployments. Existing ARM templates can also be decompiled into Bicep files.
  6. Integration with Azure CLI and Azure PowerShell- Bicep is integrated with Azure CLI and Azure PowerShell, making it easy to deploy Bicep files and manage Azure resources using familiar tools.

To create a new VM using a Bicep file, you must define the required Azure resources and their configurations, such as the virtual network, subnet, network security group, public IP address, and the VM itself. 

Here's a simple Bicep file example that creates a basic VM,

param vmName string
param adminUsername string
param adminPassword string {
    secure: true
}
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2020-06-01' = {
    name: '${vmName}Vnet'
    location: resourceGroup().location
    properties: {
        addressSpace: {
            addressPrefixes: ['10.0.0.0/16']
        }
    }
}
resource subnet 'Microsoft.Network/virtualNetworks/subnets@2020-06-01' = {
    name: '${virtualNetwork.name}/default'
    properties: {
        addressPrefix: '10.0.0.0/24'
    }
}
resource publicIP 'Microsoft.Network/publicIPAddresses@2020-06-01' = {
    name: '${vmName}PublicIP'
    location: resourceGroup().location
    properties: {
        publicIPAllocationMethod: 'Dynamic'
    }
}
resource networkInterface 'Microsoft.Network/networkInterfaces@2020-06-01' = {
    name: '${vmName}Nic'
    location: resourceGroup().location
    properties: {
        ipConfigurations: [{
            name: 'ipconfig1'
            properties: {
                privateIPAllocationMethod: 'Dynamic'
                subnet: {
                    id: subnet.id
                }
                publicIPAddress: {
                    id: publicIP.id
                }
            }
        }]
    }
}
resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = {
    name: vmName
    location: resourceGroup().location
    properties: {
        hardwareProfile: {
            vmSize: 'Standard_DS1_v2'
        }
        osProfile: {
            computerName: vmName
            adminUsername: adminUsername
            adminPassword: adminPassword
        }
        storageProfile: {
            imageReference: {
                publisher: 'MicrosoftWindowsServer'
                offer: 'WindowsServer'
                sku: '2016-Datacenter'
                version: 'latest'
            }
            osDisk: {
                createOption: 'FromImage'
            }
        }
        networkProfile: {
            networkInterfaces: [{
                id: networkInterface.id
            }]
        }
    }
}

To deploy the VM using the Bicep file, follow these steps,

  1. Install the Azure CLI and Bicep CLI if you haven't already. Instructions can be found here: https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/install
  2. Save the Bicep code above in a file named vm.bicep.
  3. Use the Azure CLI to authenticate with your Azure account: az login
  4. Create a resource group for your deployment: az group create --name myResourceGroup --location eastus
  5. Deploy the Bicep file to create the VM: az deployment group create --resource-group myResourceGroup --template-file vm.bicep --parameters vmName=myVmName adminUsername=myAdminUsername adminPassword=myAdminPassword

Summary

Overall, Bicep simplifies creating and managing Azure infrastructure, making it a valuable tool for developers and IT professionals working with Azure deployments.


Similar Articles