Lesson 5 of 5 ARM Templates and Bicep Files

Exporting Templates and Converting Them to Bicep

This lesson explains the two ways to export templates and how to convert between JSON and Bicep.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between exporting from a resource group and from deployment history.
  • Describe how to decompile JSON to Bicep and build Bicep to JSON.

Exporting templates

There are two export sources with different results. Export template on a resource group or a selection of resources generates a template from the resources' current state, in ARM JSON or Bicep. It captures resources however they were created, including manual changes, but the output usually needs cleaning: values are hard-coded unless Include parameters is selected, some resource types and properties cannot be exported (the portal lists warnings), and read-only properties may be included. Up to 200 resources can be exported at once.

Exporting from deployment history (the Deployments page, Template tab, or az deployment group export) returns the original template that was deployed, with its parameters and expressions intact, but it does not include changes made after the deployment or resources created by other means. Exported templates can be downloaded, added to a library or saved as template specs.

Export from resource group or resource
ReflectsCurrent state of resources
Parameters and expressionsMostly hard-coded values
Includes later manual changesYes
Choose when Capture resources created manually
Export from deployment history
ReflectsThe template as originally deployed
Parameters and expressionsPreserved
Includes later manual changesNo
Choose when Reuse a previous deployment exactly

The following diagram shows the steps from the resource group to a tested Bicep file.

Capturing a hand-built environment as Bicep. Export template on the resource group reflects the resources' current state, including months of manual changes, and can produce Bicep directly or JSON for az bicep decompile. The location and names are then parameterized and tested with what-if. Deployment history returns only the original deployment.
The file name is illustrative.
Sort into groups

Which export source?

Exam skills 3.1.5

Decide which export source each statement describes.

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

Converting between JSON and Bicep

az bicep decompile --file template.json (or bicep decompile) converts an ARM JSON template to a .bicep file. Decompilation is best-effort: complex expressions may produce warnings, and symbolic names generated from resource names may need renaming, so the result should be reviewed and tested with what-if. az bicep build --file main.bicep does the opposite, producing the ARM JSON that Resource Manager deploys. In Visual Studio Code, the Bicep extension can decompile a file and paste JSON as Bicep. The steps below summarize a typical migration.

  1. Export

    Export the resource group's template (or take the existing JSON template).

  2. Decompile

    Run az bicep decompile to create a .bicep file.

  3. Clean up

    Fix warnings, add parameters for hard-coded values, remove read-only properties and rename symbolic names.

  4. Validate

    Run what-if against the existing resource group and confirm that no unexpected changes appear.

Put in order

From hand-built resources to Bicep

Exam skills 3.1.5

Arrange the steps of turning an existing resource group into a tested Bicep file.

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

Capturing a hand-built environment

A test environment of a VNet, two VMs and a load balancer was built manually in the portal over several months, with many configuration changes. The team wants a Bicep file that recreates it in another region. What should the administrator do?

Use Export template on the resource group, choosing Bicep (or JSON followed by az bicep decompile), because the export reflects the resources' current state including all manual changes. Then parameterize the location and names, and test with what-if. Exporting from deployment history would return only the original portal deployments without the later changes.

Check yourself

Which command converts an ARM JSON template to a Bicep file?

Show answer

az bicep decompile.

Exam Tips

Remember for the exam

  • Export from a resource group captures current state; from deployment history, the original template.
  • az bicep decompile converts JSON to Bicep; az bicep build converts Bicep to JSON.
  • Exported templates usually need parameters added and warnings fixed.
  • Validate converted templates with what-if.