Skip to content

Lab 03: One template, many tools

At a glance

  • Time: about 60 minutes
  • Region: Southeast Asia
  • You create: 5 virtual networks (free) in your lab resource group
  • Clean up: delete the 5 virtual networks at the end

What you will learn

  • What an Azure Resource Manager (ARM) template is and how to read one.
  • How to deploy a template from the portal, Azure PowerShell, the Azure CLI and Bicep.
  • That templates are checked by Azure Policy like any other deployment.

What is different in this environment

You create virtual networks, which are free, in your own resource group in Southeast Asia. You also don't upload lab files: you copy the template text from this page.

Diagram: five ways to deploy the same virtual network (portal form, portal template, Azure PowerShell, Azure CLI and Bicep) into your resource group through Azure Resource Manager and Azure Policy. A sixth deployment in East US is blocked.
Five ways to deploy the same virtual network into your resource group, and the sixth that Azure Policy blocks. Select the diagram to open it full size.

Before you start

  • You are signed in to the lab directory (Getting started).
  • You know your group number N. Replace N in every name and command with it.
  • Your lab resource group is rg-CLOUD-LAB-GRP-N.

The template you will use

A template describes what you want, and Azure works out how. This one describes one virtual network with two subnets. Read it before you use it.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "type": "string",
      "metadata": { "description": "Name of the virtual network." }
    },
    "location": {
      "type": "string",
      "defaultValue": "southeastasia"
    },
    "labTag": {
      "type": "string",
      "defaultValue": "arm-lab"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2023-09-01",
      "name": "[parameters('vnetName')]",
      "location": "[parameters('location')]",
      "tags": { "lab": "[parameters('labTag')]" },
      "properties": {
        "addressSpace": { "addressPrefixes": [ "10.30.0.0/16" ] },
        "subnets": [
          { "name": "subnet-a", "properties": { "addressPrefix": "10.30.0.0/24" } },
          { "name": "subnet-b", "properties": { "addressPrefix": "10.30.1.0/24" } }
        ]
      }
    }
  ],
  "outputs": {
    "vnetId": { "type": "string", "value": "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))]" }
  }
}

Think about it

  • Which part is the parameters section, and which values can you change at deployment time?
  • What does [parameters('vnetName')] do?
  • Which part says what to create?

Task 1: Create a virtual network in the portal and look at its template

  1. In the portal, search for Virtual networks and select + Create.
  2. Fill in:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Name vnet-N-arm1
    Region Southeast Asia
  3. Select Review + create, then Create. Wait for the deployment to complete.

  4. Open your resource group and select Settings → Deployments. Select your deployment.
  5. Select Template. This is the template the portal generated for you. Find the parameters, the resources section and the virtual network type, and compare them with the template above.
  6. Select Download and save the files.

Another way: export a template

You can also open the virtual network, then Automation → Export template. If your role doesn't allow it, use the deployment's Template tab as above.

Task 2: Deploy a template in the portal

  1. Search for Deploy a custom template and select it.
  2. Select Build your own template in the editor.
  3. Delete everything in the editor and paste the template from above. Select Save.
  4. Fill in:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Vnet Name vnet-N-arm2
    Location southeastasia
  5. Select Review + create, then Create.

  6. When it completes, open the resource group. You should see vnet-N-arm1 and vnet-N-arm2. Open vnet-N-arm2 and check the two subnets and the tag lab = arm-lab.

Task 3: Deploy with Azure PowerShell

You use Cloud Shell, a command line in your browser. Keep one Cloud Shell window open for Tasks 3 to 6, because the files you create are deleted when the session ends.

  1. Select the Cloud Shell icon at the top of the portal.
  2. Select PowerShell.
  3. On Getting started, select No storage account required (an ephemeral session), choose the subscription, and select Apply.
  4. Create the template file. Paste this whole block, which saves the template from above as template.json:

    @'
    PASTE THE TEMPLATE HERE
    '@ | Set-Content template.json
    

    Replace the line PASTE THE TEMPLATE HERE with the template, and make sure @' and '@ stay on their own lines.

  5. Create a parameters file:

    @'
    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": { "value": "southeastasia" },
        "labTag": { "value": "arm-lab" }
      }
    }
    '@ | Set-Content parameters.json
    
  6. Check that you are in the right place:

    Get-AzContext
    Get-Content template.json | Select-Object -First 5
    
  7. Deploy the template:

    $rg = "rg-CLOUD-LAB-GRP-N"
    New-AzResourceGroupDeployment -ResourceGroupName $rg -TemplateFile template.json -vnetName "vnet-N-arm3"
    
  8. Check the result:

    Get-AzVirtualNetwork -ResourceGroupName $rg | Format-Table Name, Location, ProvisioningState
    

Task 4: Deploy with the Azure CLI

The Azure CLI also works in the PowerShell Cloud Shell. This time you use the parameters file.

az deployment group create --resource-group $rg --template-file template.json --parameters parameters.json --parameters vnetName=vnet-N-arm4
az network vnet list --resource-group $rg --output table

Note

The second --parameters sets one value on the command line. Values given later override earlier ones.

Task 5: Deploy with Bicep

Bicep is a shorter way to write the same thing. Azure turns it into an ARM template when you deploy.

  1. Create the Bicep file:

    @'
    param vnetName string
    param location string = 'southeastasia'
    param labTag string = 'arm-lab'
    
    resource vnet 'Microsoft.Network/virtualNetworks@2023-09-01' = {
      name: vnetName
      location: location
      tags: {
        lab: labTag
      }
      properties: {
        addressSpace: {
          addressPrefixes: [
            '10.30.0.0/16'
          ]
        }
        subnets: [
          {
            name: 'subnet-a'
            properties: {
              addressPrefix: '10.30.0.0/24'
            }
          }
          {
            name: 'subnet-b'
            properties: {
              addressPrefix: '10.30.1.0/24'
            }
          }
        ]
      }
    }
    
    output vnetId string = vnet.id
    '@ | Set-Content main.bicep
    
  2. Deploy it:

    az deployment group create --resource-group $rg --template-file main.bicep --parameters vnetName=vnet-N-arm5
    az network vnet list --resource-group $rg --output table
    

    The first time, the Azure CLI may install the Bicep tool. This takes a minute.

Think about it

Compare main.bicep with template.json. Which is shorter? Which one is easier to read?

Task 6: See a policy block a template

Templates obey Azure Policy. Try to deploy the same virtual network in a region that isn't allowed.

az deployment group create --resource-group $rg --template-file template.json --parameters parameters.json --parameters vnetName=vnet-N-arm6 location=eastus

The deployment fails with RequestDisallowedByPolicy. Find the text that starts with [LAB-. It says which rule blocked the deployment and shows your group name. Nothing is created.

Task 7: Clean up

  1. Delete the five virtual networks:

    Get-AzVirtualNetwork -ResourceGroupName $rg | Where-Object Name -like "vnet-N-arm*" | Remove-AzVirtualNetwork -Force
    
  2. Check that they are gone:

    Get-AzVirtualNetwork -ResourceGroupName $rg | Format-Table Name
    
  3. Close Cloud Shell. Your files are deleted with the session.

Check your work

  • You deployed the same virtual network five ways: portal wizard, portal template, PowerShell, CLI and Bicep.
  • You can point at the parameters and resources sections of a template.
  • You saw a template deployment blocked with a [LAB-...] code.
  • Your resource group has no vnet-N-arm... networks left.

Key points

  • A template is a file that describes the resources you want. Deploying the same template twice gives the same result, which makes it repeatable.
  • Parameters let you reuse one template with different values.
  • ARM templates (JSON) and Bicep do the same job. Bicep is shorter.
  • Every way of deploying goes through Azure Resource Manager, so RBAC and Azure Policy apply the same way.