Write a Bicep file, preview it with what-if and deploy it
In this task you'll deploy a Bicep file with a what-if check first.
Bicep is a simpler language for the same job as ARM templates. A Bicep file is shorter and easier to read than JSON, and Azure turns it into an ARM template for you when you deploy. The Azure CLI in Cloud Shell already understands it.
Before deploying, what-if shows exactly what would be created, changed or deleted, without changing anything. It's the safest habit to learn.
Example. Ironbridge Payments describes each new environment in a Bicep file. Every change goes through a pull request that includes the what-if output, so reviewers can see the effect before anyone deploys.
Before you start
You need: Contributor on the resource group. Time: 15 minutes.
Where to look
- Cloud Shell
- Bash
- Editor
Steps
- Open Cloud Shell in Bash. Make a file by typing
code main.bicep(or use Editor in the toolbar). - Type a small Bicep file for a free route table, one line at a time:
param location string = resourceGroup().location, thenresource rt 'Microsoft.Network/routeTables@2024-05-01' = {, thenname: 'rt-from-bicep', thenlocation: location, and a closing}. Save it. - Preview it:Type
az deployment group what-if --resource-group rg-portal-practice --template-file main.bicep. It lists rt-from-bicep with a + (create). Anything already in the group shows as * (ignore). - Deploy it:Type
az deployment group create --resource-group rg-portal-practice --template-file main.bicep. - Add a line inside the braces, such as
tags: { env: 'practice' }, save, and run what-if again. This time it shows a ~ (modify) with just the tag change. - Run the create command again, then check the tag on rt-from-bicep in the portal.
Commands
# Preview, then deploy a Bicep file
az deployment group what-if --resource-group rg-portal-practice --template-file main.bicep
az deployment group create --resource-group rg-portal-practice --template-file main.bicep
# Turn Bicep into an ARM template, or the other way round
az bicep build --file main.bicep
az bicep decompile --file template.json
Watch out for
- Cost
- Deploying is free; route tables are free too. Check what-if for anything paid before creating.
- Security
- Mark password parameters with
@secure()so they aren't shown in logs, and keep Bicep files in a repository with reviews. - Common mistakes
- Skipping what-if. Using an old or misspelt API version (the part after @). Forgetting to save the file before running the command.
Clean up
- rg-portal-practice
- rt-from-bicep
- Delete
Delete rt-from-bicep, or the resource group. Remove the file with rm main.bicep.
Words to know
- Bicep
- A short, readable language for describing Azure resources. It compiles to an ARM template.
- What-if
- A preview of a deployment's changes, without making them.
- Module
- A Bicep file used inside another one, to reuse common pieces.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On