In my previous article, Building Azure Resource Manager Templates , I covered how to get started with Azure Resource Manager templates. While they are certainly great for basic deployments, where they really shine is in their ability to allow for complex deployments. This post will cover the Custom Script Extension and how they can be used to configure Virtual Machines during the deployment process. Note: This article makes the assumption that you are familiar with the Azure portal and Visual Studio. I am not writing a full step-by-step article. While I will outline all of the things that need to happen, I am not doing a “click here” walk-through.
When I was working on my ARM Template to deploy SQL Server 2016 with the AdventureWorks sample databases installed, I needed a way to configure the virtual machine once it was installed. This is done using the Custom Script for Windows Extension. It is dependent upon the creation of the virtual machine, as can be seen from the image below and requires that the virtual machine be created before adding the extension.
After adding the Custom Script Extension, a resource is added to the virtual machine in the ARM template with they type “extensions”. The code can be seen below. It shows up as nested in the JSON Outline window. It also creates a customScripts folder in the solution. In the case of a Windows extension this file is a PowerShell or .ps1 file.
From the custom script, I can perform a host of different actions based on PowerShell. The code below performs a number of actions. It creates a folder structure, downloads files, creates and executes a PowerShell function to extract the zip files, moves files, executes T-SQL, and opens firewall ports.
By default the custom script is located in the solution but it does not have to be. In the code example below, I actually call the script from GitHub. Note the fileUris: link.
With this post we showed how we can create a virtual machine and customize it through the use of Azure Resource Manager templates. In future posts we will explore how to expand the use of Azure Resource Manager templates to create complex services that include multiple Azure Resources and services. Are you using Azure Resource Manager templates in your environment? We would love to hear about it in the comments below.
If you like the content on my blog, I also blog on the US Azure and Data Analytics Partner Blogs. I encourage you to check those out for more great resources. Also don’t forget to follow me on Twitter as much of what I talk about is related to Azure.