快速入门:使用 Bicep 创建 Azure 应用程序配置存储

本快速入门指南介绍如何使用 Bicep 来执行以下操作:

  • 部署应用程序配置存储区。
  • 在应用程序配置存储中创建键值。
  • 读取应用程序配置存储的密钥值。

Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。

先决条件

如果没有 Azure 订阅,请在开始之前先创建一个免费帐户

授权

要使用 Bicep 文件管理 Azure 应用程序配置资源,需要 Azure 资源管理器角色,例如参与者或所有者。 当配置存储的 ARM 身份验证模式设置为 直通 ARM 身份验证模式时,访问 Azure 应用配置数据(键值、快照)需要 Azure 资源管理器角色以及额外的 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 chinaeast2
    az deployment group create --resource-group <ResourceGroupName> --template-file main.bicep --parameters configStoreName=<AppConfigurationStoreName>
    

    部署完成后,应会看到一条指出部署成功的消息。

查看已部署的资源

使用 Azure CLI 或 Azure PowerShell 列出资源组中已部署的资源。

az resource list --resource-group <ResourceGroupName>

还可使用 Microsoft Azure 门户列出资源:

  1. 登录到 Azure 门户。
  2. 在搜索框中,输入应用程序配置,然后从列表中选择应用程序配置
  3. 选择新创建的应用程序配置资源。
  4. “操作”下,选择“配置资源管理器”
  5. 验证有两个键值。

清理资源

如果不再需要资源组及其资源,请使用 Azure CLI 或 Azure PowerShell 将其删除。

az group delete --name <ResourceGroupName>

还可以使用 Microsoft Azure 门户删除资源组:

  1. 导航到资源组。
  2. 选择“删除资源组”。
  3. 将显示一个选项卡。 输入资源组名称,然后选择“删除”

后续步骤

若要了解如何将功能标志和 密钥保管库 引用添加到应用程序配置存储,请查看 ARM 模板示例。