Lesson 3 of 5 ARM Templates and Bicep Files

Adapting Templates and Bicep Files

Administrators often adapt existing templates. This lesson explains common modifications, loops, conditions, existing resources, modules and parameter files.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how to parameterize hard-coded values.
  • Explain loops and conditional deployment in JSON and Bicep.
  • Explain how parameter files and Key Vault references handle values and secrets.

Common changes

The most frequent modification is parameterizing a hard-coded value, such as a VM size or location, so that one template serves several environments. In JSON, this means adding the parameter definition and replacing the value with [parameters('vmSize')]; in Bicep, adding param vmSize string = 'Standard_B2s' and using vmSize. Unique names are built with uniqueString(resourceGroup().id), which returns the same 13-character string for the same resource group. A new resource is added with the correct type and API version, and with a dependency on any resource it uses. Outputs are added to return values for later steps.

Scenario questions

Making a template reusable

Exam skills 3.1.2 · 3.1.3

Choose the change that makes a template fit each new requirement.

Log in to try this activity Interactive activities are free with an account.

Loops and conditions

To deploy several instances, JSON uses the copy element with a count and copyIndex() in names; Bicep uses a for expression. To deploy a resource only sometimes, JSON uses the condition property and Bicep uses if. The following snippets show both in Bicep.

param count int = 3
param deployBastion bool = false

resource disks 'Microsoft.Compute/disks@2024-03-02' = [for i in range(0, count): {
  name: 'data-disk-${i}'
  location: resourceGroup().location
  sku: { name: 'StandardSSD_LRS' }
  properties: { creationData: { createOption: 'Empty' }, diskSizeGB: 64 }
}]

resource bastionIp 'Microsoft.Network/publicIPAddresses@2024-05-01' = if (deployBastion) {
  name: 'pip-bastion'
  location: resourceGroup().location
  sku: { name: 'Standard' }
  properties: { publicIPAllocationMethod: 'Static' }
}

// Use an existing VNet without redeploying it
resource vnet 'Microsoft.Network/virtualNetworks@2024-05-01' existing = {
  name: 'vnet-hub'
}

The existing keyword references a resource that is already deployed so that its properties can be used without managing it. Modules split a large deployment into reusable files, such as a network module and a VM module, and can deploy to a different resource group or subscription with the scope property.

The following diagram shows how parameters and a loop turn one hard-coded VM into a configurable set of VMs.

Turning one hard-coded VM into a configurable set. Two parameters supply the count, defaulting to 3, and the size, defaulting to Standard_B2s; the VM and its NIC become loops over range(0, vmCount) with names vm-web-0, vm-web-1 and so on, and the NIC loop uses the same index so each VM references its own NIC.
The resource type is abbreviated.
Sort into groups

Loops, conditions and modules

Exam skills 3.1.2 · 3.1.3

Choose the construct that each requirement needs.

Log in to try this activity Interactive activities are free with an account.

Parameter files and secrets

Environment-specific values belong in parameter files: JSON files with a parameters object, or Bicep parameter files (.bicepparam) that start with using 'main.bicep'. Secrets should never appear in templates or parameter files in plain text; a JSON parameter file can reference a Key Vault secret by vault ID and secret name (the vault must allow template deployment access), and Bicep can use getSecret() on an existing key vault when passing a value to a module.

True or false

Parameter files and secrets

Exam skills 3.1.2 · 3.1.3

Check how environment values and secrets reach a deployment.

Log in to try this activity Interactive activities are free with an account.
Worked example

Deploying three VMs from one definition

A Bicep file deploys one VM named vm-web with a hard-coded size Standard_B2s. The administrator must change it to deploy a configurable number of VMs named vm-web-0, vm-web-1 and so on, with the size chosen at deployment and defaulting to Standard_B2s. What changes are needed?

Add param vmCount int = 3 and param vmSize string = 'Standard_B2s', change the VM resource (and its NIC) to a for i in range(0, vmCount) loop with the name 'vm-web-${i}', and set hardwareProfile.vmSize to vmSize. The NIC loop must use the same index so that each VM references its own NIC, for example nics[i].id. A condition would deploy one VM or none, not several.

Check yourself

Which Bicep keyword references a resource that is already deployed without managing it?

Show answer

existing.

Exam Tips

Remember for the exam

  • Repeat resources: copy in JSON, for loops in Bicep.
  • Deploy optionally: condition in JSON, if in Bicep.
  • Reference without managing: the existing keyword.
  • Pass secrets through Key Vault references, never in plain text.