There are many ways to deploy infrastructure and resources in Microsoft Azure. When you first get started, you will likely do this manually in the Azure Portal. However, as Azure resources and configuration needs to be deployed at scale, you need to use infrastructure as code to make sure resources are deployed correctly and consistently.
Microsoft Azure has a couple of Infrastructure as Code tools that can help with this – the Azure Resource Template (ARM template) and Bicep files. Let’s see how to deploy both in Microsoft Azure. We will look at deploying using the command line and the Azure Portal.
Brief review of ARM Templates and Bicep Files
ARM templates are JSON files or also they can be Bicep files that you can use to deploy resources. As the name “template” defines, these can be used to deploy infrastructure consistently across your Microsoft Azure environment. They are declarative in nature. This means they describe how the infrastructure needs to look and then when deployed, the infrastructure is created or modified to “look” like the description.
Bicep is an easier syntax to master compared to the JSON files in traditional ARM templates. Bicep has a more straightforward description of infrastructure and most find it easier than reading and validating JSON code.
Why Use ARM Templates or Bicep Files?
Note the following benefits of using ARM templates and Bicep files when deploying your Azure infrastructure.
- Make your infrastructure consistent: You can describe your infrastructure in code, which makes sure it is deployed consistently and without the common human-errors that result from deploying infrastructure manually
- Reuse code: You can create modular templates or Bicep files that can be reused so you don’t have to reinvent the wheel
- Automate the environment: You can use CI/CD pipelines to automate resource deployments
- Version Control: You can use version control systems like Git which has many benefits, including versioning, change tracking, and using CI/CD pipelines
Example ARM Template
Below is an example of an ARM template that deploys a virtual network with a subnet shown in the official Microsoft documentation here: Deploy template – Azure portal.
{
“$schema”: “https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“vnetName”: {
“type”: “string”,
“defaultValue”: “VNet1”,
“metadata”: {
“description”: “VNet name”
}
},
“vnetAddressPrefix”: {
“type”: “string”,
“defaultValue”: “10.0.0.0/16”,
“metadata”: {
“description”: “Address prefix”
}
},
“subnetPrefix”: {
“type”: “string”,
“defaultValue”: “10.0.0.0/24”,
“metadata”: {
“description”: “Subnet Prefix”
}
},
“subnetName”: {
“type”: “string”,
“defaultValue”: “Subnet1”,
“metadata”: {
“description”: “Subnet Name”
}
},
“location”: {
“type”: “string”,
“defaultValue”: “[resourceGroup().location]”,
“metadata”: {
“description”: “Location for all resources.”
}
}
},
“resources”: [
{
“type”: “Microsoft.Network/virtualNetworks”,
“apiVersion”: “2021-08-01”,
“name”: “[parameters(‘vnetName’)]”,
“location”: “[parameters(‘location’)]”,
“properties”: {
“addressSpace”: {
“addressPrefixes”: [
“[parameters(‘vnetAddressPrefix’)]”
]
},
“subnets”: [
{
“name”: “[parameters(‘subnetName’)]”,
“properties”: {
“addressPrefix”: “[parameters(‘subnetPrefix’)]”
}
}
]
}
}
]
}
ARM Templates
To deploy an ARM template, you can use the Azure CLI, PowerShell, or the Azure portal. Below is an example of using the Azure CLI:
az deployment group create –resource-group myResourceGroup –template-file template.json –parameters storageAccountName=myStorageAccount
Using the Azure Portal
You can deploy your ARM template using the Azure portal. Let’s look at that now. Log in to the Azure portal and search for “deploy a custom template”. This should find the Deploy a custom template wizard. Select the Build your own template in the editor.
Note also below, there are more specific templates available if you want to deploy a Linux virtual machine, Windows virtual machine, web app, SQL database, or Azure landing zone.
Click the Review + Create button:
Bicep
A Bicep file has a simpler syntax compared to ARM templates. It uses a declarative syntax to define resources, parameters, variables, and outputs.
Example Bicep File
param storageAccountName string
param location string = ‘West US’resource storageAccount ‘Microsoft.Storage/storageAccounts@2021-04-01’ = {
name: storageAccountName
location: location
sku: {
name: ‘Standard_LRS’
}
kind: ‘StorageV2’
}
Deploying a Bicep File
You can deploy a Bicep file using the Azure CLI. The Bicep file is automatically transpiled to an ARM template during deployment.
az deployment group create –resource-group myResourceGroup –template-file template.bicep –parameters storageAccountName=myStorageAccount
Key Differences Between ARM Templates and Bicep Files
- Syntax: Bicep provides a more readable and concise syntax compared to the JSON format of ARM templates.
- Can be modular: Bicep allows admins to have an easier way for modular code and code reuse.
- Learning Curve: Bicep is easier to learn than JSON
- Tooling: Bicep has better tools that can support coding, including VS Code extensions for syntax highlighting and code checking
Best Practices for Using ARM Templates and Bicep Files
- Use Parameters and Variables: Use parameters and variables in your templates
- Modularize Your Templates: Use smaller more manageable modules
- Validate Your Templates: Use tools like ARM Template Toolkit (arm-ttk) or Bicep linter to validate your files, along with Visual Studio Code (VS Code)
- Version Control: Store your templates or Bicep files in a version control system like Git to track changes
- Documentation: Document your templates or Bicep files to make it easier for others to understand
Wrapping up
Deploying resources using ARM templates or Bicep files is a great way to automate infrastructure deployment. Avoiding deploying things manually is a great standard for admins to live by when managing and configuring their Azure estate. Note the advantages to using infrastructure as code for configuring and deploying your Microsoft Azure environment. Make sure to understand the characteristics of both ARM templates and Bicep and how you can deploy these from the command line and using the Azure Portal deploy a custom template wizard.
Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.