Skip to content

Instantly share code, notes, and snippets.

@maskati
Created September 23, 2024 07:01
Show Gist options
  • Save maskati/2dc21b9331cffe1171312dfd408306f8 to your computer and use it in GitHub Desktop.
Save maskati/2dc21b9331cffe1171312dfd408306f8 to your computer and use it in GitHub Desktop.
Azure Bicep export

Azure has an upcoming capability to export resource templates from the Azure Portal in Bicep format. This capability is actually already added to a new version of the Export Template REST API operation. You can test this by setting the appropriate outputFormat parameter which takes a value of either Json or Bicep from the Azure.Deployments.Core.Entities.ExportTemplateOutputFormat enumeration. Below is a PowerShell script to export a resource group as a Bicep template without local ARM decompilation.

$resourcegroup = 'MyResourceGroupName'
$groupid = az group show --name $resourcegroup --query id -o tsv
$token = az account get-access-token --query accessToken -o tsv|convertto-securestring -force -asplaintext
irm -method post -authentication bearer -token $token -uri "https://management.azure.com${groupid}/exportTemplate?api-version=2024-06-01-preview" -contenttype 'application/json' -body (@{resources=@('*');outputFormat='Bicep'}|convertto-json -c) -responseheadersvariable bicep_headers
do{start-sleep -s 1;$bicep = irm -method get -authentication bearer -token $token -uri $bicep_headers.location[0] -statuscodevariable bicep_status;write-progress -activity "Waiting for HTTP 200" -status "$(get-date -f 'o') got HTTP $bicep_status"}until($bicep_status -eq 200);
$bicep|select -exp output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment