共用方式為


快速入門:使用 ARM 範本建立 Azure 應用程式組態存放區

本快速入門說明如何:

  • 使用 Azure Resource Manager 範本 (ARM 範本) 部署應用程式組態存放區。
  • 使用 ARM 範本在應用程式組態存放區中建立索引鍵/值。
  • 從 ARM 範本讀取應用程式組態存放區中的索引鍵/值。

提示

功能旗標和 Key Vault 參考是特殊類型的索引鍵/值。 如需使用 ARM 範本來建立這些項目的範例,請參閱後續步驟

Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。

如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。

將 Resource Manager 範本部署至 Azure 的按鈕。

必要條件

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

授權

管理 ARM 範本內的 Azure 應用程式組態 資源需要 Azure Resource Manager 角色,例如參與者或擁有者。 存取 Azure 應用程式組態 數據(索引鍵/值、快照集)需要 Azure Resource Manager 角色,並在傳遞 ARM 驗證模式下 Azure 應用程式組態 數據平面角色

重要

設定 ARM 驗證模式需要 應用程式組態 控制平面 API 版本或更新版本2023-08-01-preview

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本。 其會建立具有兩個索引鍵/值的新應用程式組態存放區。 然後,使用 reference 函式來輸出兩個索引鍵/值資源的值。 以這種方式讀取索引鍵的值,可讓該值使用於範本中的其他位置。

本快速入門使用 copy 元素來建立索引鍵/值資源的多個執行個體。 若要深入了解 copy 元素,請參閱 ARM 範本中的資源反覆運算

重要

此範本需要 應用程式組態 控制平面 API 版本或更新版本2022-05-01。 此版本會使用 reference 函式來讀取索引鍵/值。 從版本 2020-07-01-preview 開始,無法使用先前版本中用來讀取索引鍵/值的 listKeyValue 函式。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.30.23.60470",
      "templateHash": "539800420350662594"
    }
  },
  "parameters": {
    "configStoreName": {
      "type": "string",
      "defaultValue": "[format('appconfig{0}', uniqueString(resourceGroup().id))]",
      "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": "2024-05-01",
      "name": "[parameters('configStoreName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "standard"
      }
    },
    {
      "copy": {
        "name": "configStoreKeyValue",
        "count": "[length(parameters('keyValueNames'))]"
      },
      "type": "Microsoft.AppConfiguration/configurationStores/keyValues",
      "apiVersion": "2024-05-01",
      "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]), '2024-05-01').value]"
    },
    "reference_key_value_object": {
      "type": "object",
      "value": {
        "name": "[parameters('keyValueNames')[1]]",
        "properties": "[reference(resourceId('Microsoft.AppConfiguration/configurationStores/keyValues', parameters('configStoreName'), parameters('keyValueNames')[1]), '2024-05-01')]"
      }
    }
  }
}

範本中定義了兩個 Azure 資源:

提示

keyValues 資源的名稱是索引鍵和標籤的組合。 索引鍵和標籤是以 $ 分隔符號聯結。 標籤是選擇性的。 在上述範例中,名稱為 myKeykeyValues 資源會建立不含標籤的索引鍵/值。

百分比編碼 (也稱為 URL 編碼) 可讓金鑰或標籤包含 ARM 範本資源名稱中不允許的字元。 % 不是允許的字元,因此會在其位置使用 ~。 若要正確地進行名稱編碼,請遵循下列步驟:

  1. 套用 URL 編碼
  2. ~ 取代為 ~7E
  3. % 取代為 ~

例如,若要使用索引鍵名稱 AppName:DbEndpoint 和標籤名稱 Test建立索引鍵/值組,則資源名稱應該為 AppName~3ADbEndpoint$Test

注意

應用程式組態可讓您從虛擬網路透過私人連結進行索引鍵/值資料存取。 根據預設,此功能啟用時,將會拒絕所有透過公用網路存取應用程式組態資料的要求。 由於 ARM 範本是在您的虛擬網路外執行的,因此不允許從 ARM 範本進行資料存取。 若要在使用私人連結時允許從 ARM 範本進行資料存取,您可以使用下列 Azure CLI 命令來啟用公用網路存取。 在此案例中,請務必考量啟用公用網路存取的安全性影響。

az appconfig update -g MyResourceGroup -n MyAppConfiguration --enable-public-network true

部署範本

選取以下影像來登入 Azure 並開啟範本。 範本會建立具有兩個索引鍵/值的應用程式組態存放區。

將 Resource Manager 範本部署至 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 ..."

檢閱已部署的資源

  1. 登入 Azure 入口網站
  2. 在 Azure 入口網站搜尋方塊中,鍵入應用程式組態。 從清單中選取 [應用程式組態]
  3. 選取新建立的應用程式組態資源。
  4. 在 [作業] 之下,按一下 [組態總管]
  5. 確認有兩個索引鍵/值存在。

清除資源

若不再需要,可刪除資源群組、應用程式組態存放區和所有相關資源。 如果您打算在未來使用應用程式組態存放區,則可跳過刪除動作。 如果您不要繼續使用此存放區,請執行下列 Cmdlet,以刪除本快速入門所建立的所有資源:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
Remove-AzResourceGroup -Name $resourceGroupName
Write-Host "Press [ENTER] to continue..."

下一步

如需了解如何將功能旗標和金鑰保存庫參考新增至應用程式組態存放區,請檢查下列 ARM 範本範例。