How to deploy everything with the click of a button or with a single line of command

Motivation:
We have all faced this. While we are testing a new service or a new library, we need to make a fresh new VM that would eat our precious minutes every day. Isn’t there something that could do this automatically? Well, fear not, Azure Templates is here to solve this problem. In this blog, I would be writing on how to create templates for your resource so that you would deploy them with some clicks in the future.
Prerequisites:
- An Azure account with Azure Virtual Machine Service.
- Azure SSH Key Service
- (Optional) Azure CLI
Azure Template Creation:
There are different ways to create the template but we will go through the easy and preferred way. So, let’s create a VM in the most traditional way, through the Azure portal. But before that, we need to create an SSH key. Search for the ‘SSH Key’ service and then create it.

Now Search for Virtual Machine and then click on it. Don’t forget to select the key that you just created in the VM.

Let’s also add the auto-shutdown feature to this VM.

Now, let’s add the most important feature for this VM, the Custom data. This is the place where we add scripts for the machine to run on its boot uptime. I have added some script to install the nvidia docker image into the machine.

Let’s also add some tags.

Here, we don’t click on Create, rather we click on ‘Download a template for Automation‘ which is right beside the ‘Create’ button.

It will prompt you to a new page. Click on the ‘Download’ to download your template.

You will get a single zip file called ‘template.zip’ and then inside that zip file, you will get two files called ‘template.json’ and ‘parameters.json’. The template is your schema and the parameters contain all your selected values. Now open your paramters.json and check it. You will find something missing, adminPublicKey and custom data. This is intentional as Microsoft doesn’t want your sensitive information to be included in the JSON which is a very good thing for Microsoft to do. You now have to add that in your JSON manually.
Let’s start with the adminPublicKey. You remember the SSH key that we made. Now let’s go to that service again and you will see the public key in the front prompt of the service starting with "ssh-rsa" and ending with "=generated-by-Azure". Copy it all and then paste in on the "adminPublicKey" of your JSON.
Now it’s time for the "customData" field. Here we can’t simply copy-paste like above, we need to strictly encode it in base64. Here is a python code to encode it.
Run this code and you will get an encoded string, paste it onto the "customData" field. Save the JSON. Now for the fun part.
Azure Template Deployment:
Go to the portal again and search for ‘Deploy a custom Template‘.

After you click on it you will be redirected to another page. Click on ‘Build your own template in the editor’.

Click on ‘Load file’ and select ‘template.json’. After the selection click on ‘Save’.

Our schema is loaded, now it’s time to upload our parameters. Click on ‘Edit parameters’ and then upload ‘parameters.json’ and hit ‘Save’.

As you can see all of our selected parameters are selected and the sensitive parameters are put in the password field for security. Hit ‘Review and Create’.

Your VM is being deployed. Wait for some time.

The deployment is finished. Now let’s see the VM itself and try to go inside the VM through the SSH keys.

It worked. Now let’s see the shutdown feature that we initialized while creating the VM.

Now for the script that we loaded into the VM. My script was downloading the Nvidia docker image and then installing it so I should see spikes in the monitoring tab. Let’s check on that.

As you can see there is spiked on all four of the major metrics which proves that the backend script is running inside the VM. Everything worked.
Conclusion:
It worked as expected. We have created a flexible template for our VM which we can deploy anytime anywhere. We can also store this template in our GitHub repo to integrate with our DevOps operation. I used the VM as an example because it has unique features compared to other services. I also choose to do it in Azure Portal because it is an easy and straight forward way. After creating the template we can deploy using the Azure CLI too.
az deployment group create --resource-group medium-test-rg --name rollout01 --template-file template.json --parameters parameters.json
Just with this one command, everything will be deployed for you but for this to work, you need to be logged in with the Azure account from the host machine.

It’s succeeded. We can also deploy from PowerShell, REST API, Github, and CloudShell. You should decide from where you want to deploy according to your own requirements. Still, the choices are endless and it’s up to you for what you want to make it. If you encounter any problems or have difficulty following the steps, comment below on this post or message me at [email protected]. You can also connect with me on Linkedin and GitHub.
Resources:
[1] Azure Templates and Azure CLI: https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-cli
[2] ARM Template Documentation: https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/