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

本快速入門說明如何使用 Bicep 進行以下作業:

  • 部署應用程式組態存放區。
  • 在 App Configuration 存放區中建立鍵值。
  • 讀取 App Configuration 存放區中的鍵值。

Bicep 是使用宣告式語法來部署 Azure 資源的特定領域語言 (DSL)。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。 Bicep 能夠為您在 Azure 中的基礎結構即程式碼解決方案,提供最佳的製作體驗。

必要條件

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

授權

若要使用 Bicep 檔案管理 Azure 應用程式組態 資源,您必須具備 Azure 資源管理員角色,例如參與者或擁有者。 存取 Azure 應用程式組態資料 (索引鍵/值、快照集) 時,如果組態存放區的 ARM 驗證模式設定為傳遞 ARM 驗證模式,則需要有 Azure Resource Manager 角色與額外的 Azure 應用程式組態資料平面角色。

重要

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

檢閱 Bicep 檔案

此快速入門中使用的 Bicep 檔案是來自 Azure 快速入門範本。

附註

Bicep 檔案會使用與 ARM 範本相同的基礎引擎。 在 ARM 範本快速入門中找到的所有秘訣、附註和重要資訊都適用於此處。 使用 Bicep 檔案時,建議您參考此資訊。

@description('Specifies the name of the App Configuration store.')
param configStoreName string = 'appconfig${uniqueString(resourceGroup().id)}'

@description('Specifies the Azure location where the app configuration store should be created.')
param location string = resourceGroup().location

@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.')
param keyValueNames array = [
  'myKey'
  'myKey$myLabel'
]

@description('Specifies the values of the key-value resources. It\'s optional')
param keyValueValues array = [
  'Key-value without label'
  'Key-value with label'
]

@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.')
param contentType string = 'the-content-type'

@description('Adds tags for the key-value resources. It\'s optional')
param tags object = {
  tag1: 'tag-value-1'
  tag2: 'tag-value-2'
}

resource configStore 'Microsoft.AppConfiguration/configurationStores@2024-05-01' = {
  name: configStoreName
  location: location
  sku: {
    name: 'standard'
  }
}

resource configStoreKeyValue 'Microsoft.AppConfiguration/configurationStores/keyValues@2024-05-01' = [for (item, i) in keyValueNames: {
  parent: configStore
  name: item
  properties: {
    value: keyValueValues[i]
    contentType: contentType
    tags: tags
  }
}]

output reference_key_value_value string = configStoreKeyValue[0].properties.value
output reference_key_value_object object = {
  name: configStoreKeyValue[1].name
  properties: configStoreKeyValue[1].properties
}

Bicep 檔案中定義了兩個 Azure 資源:

使用此 Bicep 檔案時,我們會建立具有兩個不同值的金鑰,其中一個金鑰具有唯一標籤。

部署 Bicep 檔案

  1. 將 Bicep 檔案以 main.bicep 儲存至本機電腦。

  2. 使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。

    在以下範例中,請將 <ResourceGroupName> 替換為您的資源群組名稱,並將 <AppConfigurationStoreName> 替換為您的組態存放區唯一名稱。

    az group create --name <ResourceGroupName> --location eastus
    az deployment group create --resource-group <ResourceGroupName> --template-file main.bicep --parameters configStoreName=<AppConfigurationStoreName>
    

    當部署完成時,您應該會看到指出部署成功的訊息。

檢閱已部署的資源

使用 Azure CLI 或 Azure PowerShell 來列出資源群組中已部署的資源。

az resource list --resource-group <ResourceGroupName>

您也可以使用 Azure 入口網站來列出資源:

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

清除資源

不再需要時,請使用 Azure CLI 或 Azure PowerShell 來刪除資源群組及其資源。

az group delete --name <ResourceGroupName>

您可以使用 Azure 入口網站刪除資源群組:

  1. 瀏覽至您的資源群組。
  2. 選取 [刪除資源群組]。
  3. 索引標籤隨即顯示。 輸入資源群組名稱,然後選取 [刪除]。

下一步

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