Deploying an Azure Virtual Machine (VM)

You and your team have been building Azure VMs both from the Azure portal and via PowerShell. Even though you have some scripts put together to automate VM-creation, the scripts fail when they are run again.

For testing purposes, you have to completely remove some resources and run scripts again just to ensure all of the proper resources are created. You’re then creating scripts for the removal too. Before long, you and your team have way too many scripts to take care of.

You’ve heard about Infrastructure as Code and see that you could consolidate many of the scripts you have into a single ARM template.

Project overview

In this project, you will create an Azure VM from scratch and all of its dependent resources. More specifically, you will:

  1. Download and inspect an example ARM template and parameter files.

  2. Create an Azure resource group with the Azure CLI.

  3. Run an ARM deployment with the template and parameters file.

  4. See an example of template validation.

  5. Modify the parameters file to reflect personal preferences.

  6. Successfully build an Azure VM with an ARM deployment.

Downloading an Azure quickstart template

As mentioned earlier, Microsoft provides hundreds of example ARM templates known as quickstart templates. The best way to get started building ARM templates is to not start from scratch but to use examples.

You can download the very simple deployment of a Windows VM ARM template found on GitHub. Ensure you download both the azuredeploy.json (template) and azuredeploy.parameters.json (parameters) files.

Inspecting the ARM template

Open up the ARM template in VS code, and you’ll immediately see the complexity alluded to earlier. This template that Microsoft defines as “simple” is also 225 lines long. But it’s a great template to begin with after you’ve collapsed some of the branches.

Notice the resources section. Each node in the resources section correlates to a specific type of Azure resource. You can see that this template creates a storage account, a public IP address, virtual network, and so on. You can use the type attribute to eyeball what the template is defining.

The properties node is another node to pay attention to. This is the node under each resource that defines it’s specific configuration. Each properties node is unique to each resource. Within this node, the ARM template schema reference comes in handy.

Finally, you’ll see the dependsOn attribute with some resources. The dependsOn attribute is an optional attribute that defines other resources that should deploy before that one. It accepts a resource ID string. You could statically add this ID, but it’d be better to use the resourceId() function. In the code given at the end of this lesson, you can see the resourceId() function is being used frequently to find the resource ID for the resources each resource depends on.

Get hands-on with 1200+ tech skills courses.