Building Azure Resource Manager Templates

Part of my responsibility as a Partner Technology Strategist at Microsoft is to work on community motions.  I regularly publish blog articles on the Microsoft US Partner Community Azure Blog and am working on building the newly created Data & Analytics Partner Community Blog.  In March I published an article on Creating a SQL Server 2016 demo and linked to an Azure Resource Manager template I created to deploy a Windows Server virtual machine with SQL 2016 installed with the AdventureWorks database sames deployed.  In this article I am going to go through the details of setting up this ARM template and some of the tools I used to get there.

tl;dr –  If you don’t care about how the sausage was made you can grab the code from the GitHub repository.

Note: All of this code was generated in Visual Studio 2015 Enterprise but can be generated using the Community edition as well.  If you don’t wan to use Visual Studio at all you can do that as well, ARM templates are simply JSON files and can be written in your favorite text editor; at the moment mine is Atom.

Setup

With Visual Studio installed, make sure that you have Azure SDK installed as well.  This will add the Azure Resource Group template that we need.  That said, get VS running and create a new project.  Select Azure Resource Group, as seen in the image below, to get the project set up.

AzureResourceGroup

The next screen, as seen below, pops up and takes the templates one step further and allow you to get started with some pre-built configurations.  These are certainly a great place to start if you are looking to build one of those solutions.  For the our purposes we are going to create a blank template.

SelectTemplate

The Business

ScaffoldThis blank template builds out the project scaffolding with 3 main files, seen in the image
but the one where we will focus the most is on the azuredeploy.json file.  The file has the basic structure of an ARM template and if you wanted to write the JSON from scratch this is where you would do it.  The Azure team, and the broader community, have created some great references to help get you started and published them on GitHub.

As I mentioned earlier in this article, it is quite possible to use your favorite text editor to create ARM Templates but the power of VS can be seen by opening the azuredeploy.json file.  This opens the JSON Outline window that not only helps users navigate but has some great creation tools.  Clicking the cube in the upper left of that window, seen in the image below, will bring up the Add Resource dialog.

JSONOutlineThese templates can be used to create a number of items within the templates and build out the parameters and variables as well.  For my SQL 2016 VM deployment I ended up with 5 resources, 9 parameters, and 18 variables.  The full JSON code can be found here.  I would normally post it directly into the post but at 254 lines and no JSON code support for the syntax formatter it makes the post troublesome to read.

The Magic

Deploying a virtual machine through an ARM template is not necessarily where the magic happens. Using the Custom Script for Windows Extension, is where I am able to deploy the AdventureWorks databases.  The script actually does a few things and can be downloaded from GitHub.

  • Create the folder structure necessary to deploy the databases
  • Download and extract the files
  • Move the backup files to the proper location
  • Uses SQL PowerShell to execute the restore commands
  • Open the firewall ports necessary to connect to the SQL Server

Deployment

As with all things SQL and most things Azure the way you deploy an ARM template, well, depends.  if you are using Visual Studio, right clicking on the project will allow for deployment right from inside of VS.  If you prefer to stay away from Visual Studio, they can also be deployed from PowerShell using the New-AzureRmResourceGroupDeployment command or azure group deployment create from the Azure CLI.  There are additional ways and a great breakdown of how to deploy Azure Arm templates can be found on the Deploy resources with Azure Resource Manager templates documentation.

Are you using Azure Resource Manager templates are a powerful way to create templates for deployments on Azure.  Are you using ARM templates today?  I would love to hear about how you are using them in the comments below.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: