快速入門:使用ARM範本建立 Azure 應用程式組態存放區
本快速入門說明如何:
- 使用 Azure Resource Manager 範本部署 應用程式組態 存放區(ARM 範本)。
- 使用ARM樣本在 應用程式組態存放區中建立索引鍵/值。
- 從 ARM 樣本讀取 應用程式組態 存放區中的索引鍵/值。
提示
功能旗標和 金鑰保存庫 參考是索引鍵/值的特殊類型。 如需 使用ARM範例建立這些範例的後續步驟,請參閱後續步驟 。
Azure Resource Manager 範本是 JavaScript 物件表示法 (JSON) 檔案,可定義專案的基礎結構和組態。 範本使用宣告式語法。 您不需要撰寫程式設計命令順序來建立部署,即可描述預定的部署。
如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。
必要條件
如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
授權
管理 ARM 範本內的 Azure 應用程式組態 資源需要 Azure Resource Manager 角色,例如參與者或擁有者。 存取 Azure 應用程式組態 數據(索引鍵/值、快照集)需要 Azure Resource Manager 角色,並在傳遞 ARM 驗證模式下 Azure 應用程式組態 數據平面角色。
檢閱範本
本快速入門中使用的範本是來自 Azure 快速入門範本。 它會建立內含兩個索引鍵/值的新 應用程式組態 存放區。 然後,它會使用 函 reference
式來輸出兩個索引鍵/值資源的值。 以這種方式讀取索引鍵的值可讓它用於範本中的其他位置。
快速入門會 copy
使用 元素來建立索引鍵/值資源的多個實例。 若要深入瞭解 元素 copy
,請參閱 ARM範本中的資源反覆專案。
重要
此範本需要 應用程式組態 資源提供者版本或更新版本2020-07-01-preview
。 此版本會使用函 reference
式來讀取索引鍵/值。 listKeyValue
從 版本 2020-07-01-preview
開始,無法使用用來讀取舊版中索引鍵/值的函式。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.5.6.12127",
"templateHash": "2173262573838896712"
}
},
"parameters": {
"configStoreName": {
"type": "string",
"metadata": {
"description": "Specifies the name of the App Configuration store."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Specifies the Azure location where the app configuration store should be created."
}
},
"keyValueNames": {
"type": "array",
"defaultValue": [
"myKey",
"myKey$myLabel"
],
"metadata": {
"description": "Specifies the names of the key-value resources. The name is a combination of key and label with $ as delimiter. The label is optional."
}
},
"keyValueValues": {
"type": "array",
"defaultValue": [
"Key-value without label",
"Key-value with label"
],
"metadata": {
"description": "Specifies the values of the key-value resources. It's optional"
}
},
"contentType": {
"type": "string",
"defaultValue": "the-content-type",
"metadata": {
"description": "Specifies the content type of the key-value resources. For feature flag, the value should be application/vnd.microsoft.appconfig.ff+json;charset=utf-8. For Key Value reference, the value should be application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8. Otherwise, it's optional."
}
},
"tags": {
"type": "object",
"defaultValue": {
"tag1": "tag-value-1",
"tag2": "tag-value-2"
},
"metadata": {
"description": "Adds tags for the key-value resources. It's optional"
}
}
},
"resources": [
{
"type": "Microsoft.AppConfiguration/configurationStores",
"apiVersion": "2021-10-01-preview",
"name": "[parameters('configStoreName')]",
"location": "[parameters('location')]",
"sku": {
"name": "standard"
}
},
{
"copy": {
"name": "configStoreKeyValue",
"count": "[length(parameters('keyValueNames'))]"
},
"type": "Microsoft.AppConfiguration/configurationStores/keyValues",
"apiVersion": "2021-10-01-preview",
"name": "[format('{0}/{1}', parameters('configStoreName'), parameters('keyValueNames')[copyIndex()])]",
"properties": {
"value": "[parameters('keyValueValues')[copyIndex()]]",
"contentType": "[parameters('contentType')]",
"tags": "[parameters('tags')]"
},
"dependsOn": [
"[resourceId('Microsoft.AppConfiguration/configurationStores', parameters('configStoreName'))]"
]
}
],
"outputs": {
"reference_key_value_value": {
"type": "string",
"value": "[reference(resourceId('Microsoft.AppConfiguration/configurationStores/keyValues', parameters('configStoreName'), parameters('keyValueNames')[0])).value]"
},
"reference_key_value_object": {
"type": "object",
"value": "[reference(resourceId('Microsoft.AppConfiguration/configurationStores/keyValues', parameters('configStoreName'), parameters('keyValueNames')[1]), '2021-10-01-preview', 'full')]"
}
}
}
範本中定義了兩個 Azure 資源:
- Microsoft.AppConfiguration/configurationStores:建立應用程式組態存放區。
- Microsoft.AppConfiguration/configurationStores/keyValues:在應用程式組態存放區內建立索引鍵/值。
提示
資源 keyValues
的名稱是索引鍵和標籤的組合。 索引鍵和標籤會由 $
分隔符聯結。 標籤是選擇性的。 在上述範例中 keyValues
,名稱為 myKey
的資源會建立沒有標籤的索引鍵/值。
百分比編碼,也稱為 URL 編碼,允許索引鍵或標籤包含 ARM 範本資源名稱中不允許的字元。 %
不是允許的字元,因此 ~
會用於其位置。 若要正確編碼名稱,請遵循下列步驟:
- 套用 URL 編碼
- 將
~
取代為~7E
- 將
%
取代為~
例如,若要建立具有索引鍵名稱和 AppName:DbEndpoint
標籤名稱 Test
的索引鍵/值組,資源名稱應該是 AppName~3ADbEndpoint$Test
。
注意
應用程式組態 可讓您透過虛擬網路的私人連結存取金鑰/值數據。 根據預設,啟用此功能時,會拒絕您透過公用網路 應用程式組態 數據的所有要求。 因為 ARM 範本會在虛擬網路外執行,因此不允許從 ARM 範本存取數據。 若要在使用私人連結時允許從 ARM 範本存取資料,您可以使用下列 Azure CLI 命令來啟用公用網路存取。 請務必考慮在此案例中啟用公用網路存取的安全性影響。
az appconfig update -g MyResourceGroup -n MyAppConfiguration --enable-public-network true
部署範本
選取以下影像來登入 Azure 並開啟範本。 範本會建立內含兩個索引鍵/值的 應用程式組態 存放區。
您也可以使用下列 PowerShell Cmdlet 來部署範本。 機碼值將會在PowerShell控制台的輸出中。
$projectName = Read-Host -Prompt "Enter a project name that is used for generating resource names"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.appconfiguration/app-configuration-store-kv/azuredeploy.json"
$resourceGroupName = "${projectName}rg"
New-AzResourceGroup -Name $resourceGroupName -Location "$location"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri
Read-Host -Prompt "Press [ENTER] to continue ..."
檢閱已部署的資源
- 登入 Azure 入口網站。
- 在 [Azure 入口網站 搜尋] 方塊中,輸入 應用程式組態。 從清單中選取 [應用程式組態]。
- 選取新建立的應用程式組態資源。
- 在 [作業] 底下,按兩下 [組態總管]。
- 確認有兩個索引鍵/值存在。
清除資源
不再需要時,請刪除資源群組、應用程式組態 存放區和所有相關資源。 如果您打算在未來使用 應用程式組態 存放區,您可以略過刪除它。 如果您不打算繼續使用此存放區,請執行下列 Cmdlet 來刪除本快速入門所建立的所有資源:
$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
Remove-AzResourceGroup -Name $resourceGroupName
Write-Host "Press [ENTER] to continue..."
下一步
若要瞭解如何將功能旗標和 金鑰保存庫 參考新增至 應用程式組態 存放區,請查看ARM範例下方。