使用 Azure Resource Manager 來開發 Azure Stack Hub 的範本

當您開發應用程式時,請務必要讓範本具有 Azure 與 Azure Stack Hub 之間的可攜性。 本文提供 Azure Resource Manager 範本的開發考量。 使用這些範本,您就可以建立原型應用程式並在 Azure 中測試部署,而不需要存取 Azure Stack Hub 環境。

資源提供者可用性

您計畫部署的範本必須僅使用已可用或在 Azure Stack Hub 中為預覽版的 Microsoft Azure 服務。

公用命名空間

因為 Azure Stack Hub 是裝載在您的資料中心,它的服務端點命名空間與 Azure 公用雲端不同。 因此,當您嘗試將 Resource Manager 範本部署到 Azure Stack Hub 時,以硬式編碼方式寫在 Azure Resource Manager 範本中的公用端點將會失敗。 您可以使用 referenceconcatenate 函式動態建置服務端點,以在部署期間從資源提供者擷取值。 例如,擷取primaryEndpoints.blob來動態設定osDisk.URI端點,而不是在範本中硬式編碼 blob.core.windows.net

"osDisk": {"name": "osdisk","vhd": {"uri":
"[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), '2015-06-15').primaryEndpoints.blob, variables('vmStorageAccountContainerName'),
 '/',variables('OSDiskName'),'.vhd')]"}}

API 版本控制

Azure 服務版本在 Azure 與 Azure Stack Hub 之間可能不同。 每個資源都需要 apiVersion 屬性,此屬性會定義提供的功能。 若要確保 Azure Stack Hub 中的 API 版本相容性,下列 API 版本適用於每個資源提供者:

資源提供者 apiVersion
計算 2015-06-15
網路 2015-06-152015-05-01-preview
儲存體 2016-01-012015-06-152015-05-01-preview
KeyVault 2015-06-01
App Service 2015-08-01

範本函式

Azure Resource Manager 函式提供建置動態範本所需的功能。 舉例而言,您可以將函式用在類似以下幾種工作上:

  • 串連或修剪字串。
  • 參考來自其他資源的值。
  • 在資源上進行反覆處理以部署多個執行個體。

這些函式在 Azure Stack Hub 中無法使用:

  • 跳過
  • Take

資源位置

Azure Resource Manager 範本使用 location 屬性在部署期間放置資源。 在 Azure 中,位置指的是如美國西部或南美等地區。 在 Azure Stack Hub 中,位置則不同,因為 Azure Stack Hub 位於您的資料中心。 若要確保能在 Azure 與 Azure Stack Hub 之間移轉範本,在部署個別資源時,請將資源群組位置也納入參考。 您可以使用 [resourceGroup().Location] 來執行此動作,以確保所有資源皆會繼承資源群組位置。 下列程式碼即是在部署儲存體帳戶時使用此功能的範例:

"resources": [
{
  "name": "[variables('storageAccountName')]",
  "type": "Microsoft.Storage/storageAccounts",
  "apiVersion": "[variables('apiVersionStorage')]",
  "location": "[resourceGroup().location]",
  "comments": "This storage account is used to store the VM disks",
  "properties": {
  "accountType": "Standard_LRS"
  }
}
]

後續步驟