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.
Before you start¶
- You are signed in to the lab directory (Getting started).
- You know your group number
N. ReplaceNin 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¶
- In the portal, search for Virtual networks and select + Create.
-
Fill in:
Setting Value Resource group rg-CLOUD-LAB-GRP-NName vnet-N-arm1Region Southeast Asia -
Select Review + create, then Create. Wait for the deployment to complete.
- Open your resource group and select Settings → Deployments. Select your deployment.
- Select Template. This is the template the portal generated for you. Find the parameters, the
resourcessection and the virtual network type, and compare them with the template above. - 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¶
- Search for Deploy a custom template and select it.
- Select Build your own template in the editor.
- Delete everything in the editor and paste the template from above. Select Save.
-
Fill in:
Setting Value Resource group rg-CLOUD-LAB-GRP-NVnet Name vnet-N-arm2Location southeastasia -
Select Review + create, then Create.
- When it completes, open the resource group. You should see
vnet-N-arm1andvnet-N-arm2. Openvnet-N-arm2and check the two subnets and the taglab = 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.
- Select the Cloud Shell icon at the top of the portal.
- Select PowerShell.
- On Getting started, select No storage account required (an ephemeral session), choose the subscription, and select Apply.
-
Create the template file. Paste this whole block, which saves the template from above as
template.json:@' PASTE THE TEMPLATE HERE '@ | Set-Content template.jsonReplace the line
PASTE THE TEMPLATE HEREwith the template, and make sure@'and'@stay on their own lines. -
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 -
Check that you are in the right place:
Get-AzContext Get-Content template.json | Select-Object -First 5 -
Deploy the template:
$rg = "rg-CLOUD-LAB-GRP-N" New-AzResourceGroupDeployment -ResourceGroupName $rg -TemplateFile template.json -vnetName "vnet-N-arm3" -
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.
-
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 -
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 tableThe 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¶
-
Delete the five virtual networks:
Get-AzVirtualNetwork -ResourceGroupName $rg | Where-Object Name -like "vnet-N-arm*" | Remove-AzVirtualNetwork -Force -
Check that they are gone:
Get-AzVirtualNetwork -ResourceGroupName $rg | Format-Table Name -
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
parametersandresourcessections 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.