使用 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 公用雲端不同。 因此,當您嘗試將公用端點部署到 Azure Stack Hub 時,Azure Resource Manager 範本中的硬式編碼公用端點會失敗。 您可以使用 reference
和 concatenate
函式,在部署期間從資源提供者擷取值,以動態方式建置服務端點。 例如,與其在範本中硬性編碼 blob.core.windows.net
,不如擷取 primaryEndpoints.blob,以動態方式設定 osDisk.URI 端點:
"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-15,2015-05-01-preview |
存儲 | 2016-01-01,2015-06-15,2015-05-01-preview |
KeyVault | 2015-06-01 |
應用程式服務 | 2015-08-01 |
範本函式
Azure Resource Manager 函式 提供建置動態範本所需的功能。 如,您可以將功能用於下列工作:
- 串連或截取字串。
- 引用其他資源的值。
- 資源的反覆配置以部署多個實例。
Azure Stack Hub 中無法使用這些函式:
- 跳過
- 拿
資源位置
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"
}
}
]