Azure - Introduction to ARM templates

Search for a command to run...

No comments yet. Be the first to comment.
Backtracking explained step by step with problems

Whether you're a blogger, startup, or business, blogging is a long-term strategy to build a strong presence in front of humans as well as search engines. With these small, easy-to-implement SEO tips, you can increase your blog's reach and traffic. 1....

12 Common uses of Java Streams with code examples
In the previous article, we looked at the fundamentals of session management. We also looked at two types of access tokens and how both of them are inadequate when it comes to providing strong security along with a good user experience. In this artic...

Introduction Session management is the process of maintaining a session " between a client and a server", "for a user", "for a period of time". A simple session management flow The process can be described as follows: A user authenticates with their...

While working in a cloud based environment, we are rapidly creating and deploying new resources. Azure provides few different ways to deploy new resources. Let's have a quick look:
Let's have a look at a sample template and identify its different components https://raw.githubusercontent.com/Abh1navv/azure-quickstart-templates/master/101-vm-simple-linux/azuredeploy.json
This template is used to create a Linux VM on Azure. You can find details of the template at mygithub repo. You can also check the original repo which contains several quickstart ARM templates. (My fork only has one extra commit for the purpose of this article)
Note: We usually do not create templates from scratch. There are easy ways to extract template from a running resource and modify/reuse it as per our need. Check this blog for steps to extract a template using Portal.
Let's look at a few important details from this sample.
"parameters": {
"vmName": {
"type": "string",
"defaultValue": "simpleLinuxVM",
"metadata": {
"description": "The name of you Virtual Machine."
}
},
"authenticationType": {
"type": "string",
"defaultValue": "password",
"allowedValues": [
"sshPublicKey",
"password"
],
"metadata": {
"description": "Type of authentication to use on the Virtual Machine. SSH key is recommended."
}
}
}
vmName is the variable that the user can use to set name for the new VM. Optionally user can keep it empty and a defaultValue will be used. You can also specify limits on user inputs - In the second parameter authenticationType, we are specifying that only allowedValues are "sshPublicKey" and "password"."osDiskType": "Standard_LRS",
"subnetAddressPrefix": "10.1.0.0/24",
"addressPrefix": "10.1.0.0/16",
Function example:
"namespace": "quickstart",
"members": {
"uniqueName": {
"parameters": [
{
"name": "namePrefix",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
}
}
}
}
Checks that the provided parameter namePrefix is unique. It is then used when we are setting our VM name.
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2020-06-01",
"name": "[quickstart.uniqueName(parameters('vmName'))]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('VmSize')]"
},
"storageProfile": {
"osDisk": {
"createOption": "fromImage",
"managedDisk": {
"storageAccountType": "[variables('osDiskType')]"
}
},
"imageReference": {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "[parameters('ubuntuOSVersion')]",
"version": "latest"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
}
]
},
"osProfile": {
"computerName": "[parameters('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPasswordOrKey')]",
"linuxConfiguration": "[if(equals(parameters('authenticationType'), 'password'), json('null'), variables('linuxConfiguration'))]"
}
}
}
Have a look at how the syntax uses parameters, functions and variables:
name - we are using parameter vmName which we talked about earlier and checking if its unique with function defined above.
dependsOn - defines the resources that should be available before creating the VM. It also makes use of variable networkInterfaceName to specify the network interface dependency."hostname": {
"type": "string",
"value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
}
This code will look for publicIPAddressName object and read its dnsSettings.fqdn field to return its fully qualified domain name and return it with the output variable hostname. This is very useful in CI/CD workflows where we need resource details to immediately interact with it.So we have now looked at the basic building blocks. Let's go ahead and see how to deploy the template using CLI.
Before deploying the template, make sure you are logged in to the CLI client -> select a subscription -> create resource group if it doesn't already exist.
Once we are ready, we can run the below command to deploy our templates.
az group deployment create --name "name of your deployment" --resource-group "resource-group" --template-file "./azuredeploy.json"
CLI will ask for required parameter upon execution. It will use default values wherever you do not provide values.
An alternate way is to use parameter files in which case we modify our command to point to a JSON to pick the parameters from. This also works great in automation workflows. The syntax is as below:
az group deployment create --name "name of your deployment" --resource-group "resource-group" --template-file "./azuredeploy.json" --parameters @azure.parameters.json
Note: There are multiple ways to deploy templates. CLI is just an example. For more examples, check microsoft blogs
Once all values are provided, the deployment will be created -> accepted or rejected -> deployed if valid.
Thanks for reading! Stay tuned for more on Azure.
You can connect with me at