Bagikan melalui


Decompile a JSON Azure Resource Manager template to Bicep

Artikel ini menjelaskan cara mendekompilasi templat Azure Resource Manager (templat ARM) ke file Bicep. Anda harus memasang Bicep CLI untuk menjalankan perintah konversi.

Catatan

Dari Visual Studio Code, Anda dapat langsung membuat deklarasi sumber daya dengan mengimpor dari sumber daya yang ada. Untuk informasi selengkapnya, lihat perintah Bicep.

Visual Studio Code enables you to paste JSON as Bicep. Ini secara otomatis menjalankan perintah decompile. For more information, see Paste JSON as Bicep.

Dekompilasi templat ARM akan membantu Anda memulai pengembangan Bicep. Jika Anda memiliki pustaka templat ARM dan ingin menggunakan Bicep untuk pengembangan yang akan datang, Anda dapat mendekompilasinya ke Bicep. However, you might need to revise the Bicep file to implement best practices.

Artikel ini memperlihatkan cara menjalankan perintah decompile di Azure CLI. Jika Anda tidak menggunakan Azure CLI, jalankan perintah tanpa az di awal perintah. Misalnya, az bicep decompile menjadi bicep decompile.

Dekompilasi dari JSON ke Bicep

Untuk mendekompilasi JSON templat ARM ke Bicep, gunakan:

az bicep decompile --file main.json

Perintah ini membuat file bernama main.bicep di direktori yang sama dengan main.json. If main.bicep exists in the same directory, use the --force switch to overwrite the existing Bicep file.

Anda juga dapat mendekompilasi JSON templat ARM ke Bicep dari Visual Studio Code dengan menggunakan Decompile into Bicep perintah di Visual Studio Code. Untuk informasi selengkapnya, lihat Dekompilasi ke Bicep.

Perhatian

Decompilation attempts to convert the file, but there's no guaranteed mapping from JSON ARM templates to Bicep. Anda mungkin perlu memperbaiki peringatan dan kesalahan dalam file Bicep yang dihasilkan. Jika tidak, dekompretasi dapat gagal jika konversi yang akurat tidak dimungkinkan. Buat masalah untuk melaporkan masalah atau konversi yang tidak akurat.

Proses dekompilasi dan pembuatan perintah menghasilkan templat yang setara secara fungsi. Namun, mereka mungkin tidak persis sama selama implementasi. Mengonversi templat dari JSON ke Bicep lalu kembali ke JSON dapat menghasilkan templat dengan sintaks yang berbeda dari templat asli. Saat disebarkan, templat yang dikonversi menghasilkan hasil yang sama.

Memperbaiki masalah konversi

Misalkan Anda memiliki templat ARM berikut:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "storageAccountName": "[concat('store', uniquestring(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    }
  }
}

Saat Anda mendekompilasinya, Anda memperoleh:

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_ZRS'
  'Premium_LRS'
])
@description('Storage Account type')
param storageAccountType string = 'Standard_LRS'

@description('Location for all resources.')
param location string = resourceGroup().location

var storageAccountName = 'store${uniqueString(resourceGroup().id)}'

resource storageAccount 'Microsoft.Storage/storageAccounts@2025-06-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: storageAccountType
  }
  kind: 'StorageV2'
  properties: {}
}

output storageAccountName string = storageAccountName

File hasil dekompilasi berfungsi, tetapi ada beberapa nama yang mungkin ingin Anda ubah. Variabel var storageAccountName_var memiliki konvensi penamaan yang tidak umum. Anda dapat mengubahnya menjadi:

var uniqueStorageName = 'store${uniqueString(resourceGroup().id)}'

Untuk mengganti nama di seluruh file, klik kanan nama, lalu pilih Ganti nama simbol. Anda juga dapat menggunakan hotkey F2 .

Sumber daya memiliki nama simbolis yang mungkin ingin Anda ubah. Daripada storageAccountName sebagai nama simbolik, gunakan exampleStorage.

resource exampleStorage 'Microsoft.Storage/storageAccounts@2025-06-01' = {

File yang lengkap adalah:

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_ZRS'
  'Premium_LRS'
])
@description('Storage Account type')
param storageAccountType string = 'Standard_LRS'

@description('Location for all resources.')
param location string = resourceGroup().location

var uniqueStorageName = 'store${uniqueString(resourceGroup().id)}'

resource exampleStorage 'Microsoft.Storage/storageAccounts@2025-06-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageAccountType
  }
  kind: 'StorageV2'
  properties: {}
}

output storageAccountName string = uniqueStorageName

Mengekspor templat dan mengonversi

Catatan

Anda dapat menggunakan portal Microsoft Azure untuk mengekspor file Bicep. Untuk informasi selengkapnya, lihat Menggunakan portal Microsoft Azure untuk mengekspor file Bicep.

Anda dapat mengekspor templat untuk grup sumber daya lalu meneruskannya langsung ke decompile perintah . Contoh berikut menunjukkan cara mendekompresi templat yang diekspor:

az group export --name "your_resource_group_name" > main.json
az bicep decompile --file main.json

Tampilan berdampingan

Bicep Playground memungkinkan Anda melihat templat ARM dan file Bicep yang setara secara berdampingan. Anda dapat memilih Templat Sampel untuk melihat kedua versi, atau memilih Dekompilasi untuk mengunggah templat ARM Anda sendiri dan melihat file Bicep yang setara.

Langkah berikutnya

To learn about all Bicep CLI commands, see Bicep CLI commands.