ARM 範本中的資源宣告

若要透過 Azure Resource Manager 範本 (ARM 範本) 部署資源,您需要新增資源宣告。 使用 JSON 範本中的 resources 陣列。

languageVersion 2.0 會建立 ARM JSON 範本的增強功能清單,例如將資源宣告從陣列變更為物件。 本文中顯示的大部分範例仍會使用 resources array。 如需 languageVersion 2.0 特定資訊,請參閱 使用符號名稱

注意

適用于 Visual Studio Code 的 Azure Resource Manager Tools 擴充功能目前版本無法辨識languageVersion 2.0中所做的增強功能。

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱資源宣告

範本中限制使用 800 個資源。 如需詳細資訊,請參閱範本限制

設定資源類型和版本

將資源新增至範本時,請先設定資源類型和 API 版本。 這些值會決定可用於資源的其他屬性。

下列範例顯示如何設定儲存體帳戶的資源類型和 API 版本。 此範例不會顯示完整的資源宣告。

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    ...
  }
]

設定資源名稱

每個資源都有名稱。 設定資源名稱時,請注意資源名稱的規則和限制

"parameters": {
  "storageAccountName": {
    "type": "string",
    "minLength": 3,
    "maxLength": 24
  }
},
"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    "name": "[parameters('storageAccountName')]",
    ...
  }
]

設定位置

許多資源都需要位置。 您可以透過 IntelliSense 或範本參照,判斷資源是否需要位置。 下列範例會新增用於儲存體帳戶的位置參數。

"parameters": {
  "storageAccountName": {
    "type": "string",
    "minLength": 3,
    "maxLength": 24
  },
  "location": {
    "type": "string",
    "defaultValue": "[resourceGroup().location]"
  }
},
"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-06-01",
    "name": "[parameters('storageAccountName')]",
    "location": "[parameters('location')]",
    ...
  }
]

如需詳細資訊,請參閱在 ARM 範本中設定資源位置

設定標記

您可以在部署期間將標記套用至資源。 標記可協助您以邏輯方式組織已部署的資源。 如需以不同方式指定標記的範例,請參閱 ARM 範本標記

設定資源專屬屬性

上述屬性對大部分的資源類型來說為泛型。 設定這些值之後,您需為要部署的資源類型設定專屬的屬性。

使用 IntelliSense 或範本參照,判斷哪些屬性可供使用,以及哪些是必要的屬性。 下列範例會設定儲存體帳戶的其餘屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "functions": [],
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    }
  ]
}

使用符號名稱

Bicep中,每個資源定義都有符號名稱。 符號名稱是用來從 Bicep 檔案的其他部分參考資源。 若要支援 ARM JSON 範本中的符號名稱,請新增 languageVersion 版本 2.0 ,並將資源定義從陣列變更為 物件。 針對範本指定 時 languageVersion ,必須針對根層級資源指定符號名稱。 例如:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.ContainerService/managedClusters",
      ...
    }
  ]
}

上述 JSON 可以寫入下列 JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "resources": {
    "aks": {
      "type": "Microsoft.ContainerService/managedClusters",
      ...
    }
  }
}

符號名稱須區分大小寫。 符號名稱允許的字元為字母、數位和 _。 符號名稱在範本中必須是唯一的,但可以與範本中的變數名稱、參數名稱和輸出名稱重迭。 在下列範例中,儲存體帳戶資源的符號名稱與輸出的名稱相同。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": {
    "myStorage": {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
  },
  "outputs": {
    "myStorage":{
      "type": "object",
      "value": "[reference('myStorage')]"
    }
  }
}

參考函式可以使用資源的符號名稱,如上述範例所示。 例如 reference(parameters('storageAccountName')) ,不允許參考函式再使用資源的名稱。

如果在符號名稱部署中使用 Deployments 資源 ,請使用 apiVersion 2020-09-01 或更新版本。

宣告現有的資源

使用 languageVersion 2.0 和 使用資源宣告的符號名稱,您可以宣告現有的資源。 的最上層資源屬性 "existing": true 會導致 ARM 讀取,而不是部署資源,如下列範例所示:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "languageVersion": "2.0",

  "resources": {
    "storageAccount": {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "storageacct",
      "existing": true
    }
  },
  "outputs": {
    "saBlocksPlaintext": {
      "type": "bool",
      "value": "[ reference('storageAccount').supportsHttpsTrafficOnly]"
    }
  }
}

現有的資源不需要定義 、 apiVersionname 以外的 type 任何屬性。

後續步驟