Tutorial: Create ARM templates with dependent resources

Learn how to create an Azure Resource Manager (ARM) template to deploy multiple resources and configure the deployment order. After you create the template, you deploy the template using the Cloud Shell from the Azure portal.

In this tutorial, you create a storage account, a virtual machine, a virtual network, and some other dependent resources. Some of the resources cannot be deployed until another resource exists. For example, you can't create the virtual machine until its storage account and network interface exist. You define this relationship by making one resource as dependent on the other resources. Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. When resources aren't dependent on each other, Resource Manager deploys them in parallel. For more information, see Define the order for deploying resources in ARM templates.

Resource Manager template dependent resources deployment order diagram

This tutorial covers the following tasks:

  • Open a Quickstart template
  • Explore the template
  • Deploy the template

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

To complete this article, you need:

Open a Quickstart template

Azure Quickstart Templates is a repository for ARM templates. Instead of creating a template from scratch, you can find a sample template and customize it. The template used in this tutorial is called Deploy a simple Windows VM.

  1. From Visual Studio Code, select File>Open File.

  2. In File name, paste the following URL:

    https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json
    
  3. Select Open to open the file.

  4. Select File>Save As to save a copy of the file to your local computer with the name azuredeploy.json.

Explore the template

When you explore the template in this section, try to answer these questions:

  • How many Azure resources defined in this template?
  • One of the resources is an Azure storage account. Does the definition look like the one used in the last tutorial?
  • Can you find the template references for the resources defined in this template?
  • Can you find the dependencies of the resources?
  1. From Visual Studio Code, collapse the elements until you only see the first-level elements and the second-level elements inside resources:

    Visual Studio Code Azure Resource Manager templates

    There are six resources defined by the template:

  2. Expand the first resource. It is a storage account. Compare the resource definition to the template reference.

    Visual Studio Code Azure Resource Manager templates storage account definition

  3. Expand the second resource. The resource type is Microsoft.Network/publicIPAddresses. Compare the resource definition to the template reference.

    Visual Studio Code Azure Resource Manager templates public IP address definition

  4. Expand the third resource. The resource type is Microsoft.Network/networkSecurityGroups. Compare the resource definition to the template reference.

    Visual Studio Code Azure Resource Manager templates network security group definition

  5. Expand the fourth resource. The resource type is Microsoft.Network/virtualNetworks:

    Visual Studio Code Azure Resource Manager templates virtual network dependsOn

    The dependsOn element enables you to define one resource as a dependent on one or more resources. This resource depends on one other resource:

    • Microsoft.Network/networkSecurityGroups
  6. Expand the fifth resource. The resource type is Microsoft.Network/networkInterfaces. The resource depends on two other resources:

    • Microsoft.Network/publicIPAddresses
    • Microsoft.Network/virtualNetworks
  7. Expand the sixth resource. This resource is a virtual machine. It depends on two other resources:

    • Microsoft.Storage/storageAccounts
    • Microsoft.Network/networkInterfaces

The following diagram illustrates the resources and the dependency information for this template:

Visual Studio Code Azure Resource Manager templates dependency diagram

By specifying the dependencies, Resource Manager efficiently deploys the solution. It deploys the storage account, public IP address, and virtual network in parallel because they have no dependencies. After the public IP address and virtual network are deployed, the network interface is created. When all other resources are deployed, Resource Manager deploys the virtual machine.

Deploy the template

  1. Sign in to the Azure Cloud Shell

  2. Choose your preferred environment by selecting either PowerShell or Bash (for CLI) on the upper left corner. Restarting the shell is required when you switch.

    Azure portal Cloud Shell upload file

  3. Select Upload/download files, and then select Upload. See the previous screenshot. Select the file you saved earlier. After uploading the file, you can use the ls command and the cat command to verify the file is uploaded successfully.

  4. Run the following PowerShell script to deploy the template.

    echo "Enter a project name that is used to generate resource group name:" &&
    read projectName &&
    echo "Enter the location (i.e. centralus):" &&
    read location &&
    echo "Enter the virtual machine admin username:" &&
    read adminUsername &&
    echo "Enter the DNS label prefix:" &&
    read dnsLabelPrefix &&
    resourceGroupName="${projectName}rg" &&
    az group create --name $resourceGroupName --location $location &&
    az deployment group create --resource-group $resourceGroupName --template-file "$HOME/azuredeploy.json" --parameters adminUsername=$adminUsername dnsLabelPrefix=$dnsLabelPrefix
    
  5. RDP to the virtual machine to verify the virtual machine has been created successfully.

Clean up resources

When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  1. From the Azure portal, select Resource group from the left menu.
  2. Enter the resource group name in the Filter by name field.
  3. Select the resource group name. You'll see a total of six resources in the resource group.
  4. Select Delete resource group from the top menu.

Next steps

In this tutorial, you developed and deployed a template to create a virtual machine, a virtual network, and the dependent resources. To learn how to use deployment scripts to perform pre/post deployment operations, see: