快速入门:使用 ARM 模板创建 Azure 应用程序配置存储Quickstart: Create an Azure App Configuration store by using an ARM template

本快速入门介绍如何:This quickstart describes how to :

  • 使用 Azure 资源管理器模板(ARM 模板)部署应用程序配置存储。Deploy an App Configuration store using an Azure Resource Manager template (ARM template).
  • 使用 ARM 模板在应用程序配置存储中创建键值。Create key-values in an App Configuration store using ARM template.
  • 从 ARM 模板读取应用程序配置存储中的键值。Read key-values in an App Configuration store from ARM template.

ARM 模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it.

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. Azure 门户中会打开模板。The template will open in the Azure portal.

部署到 AzureDeploy to Azure

先决条件Prerequisites

如果没有 Azure 订阅,请在开始前创建一个试用版订阅If you don't have an Azure subscription, create a trial subscription before you begin.

查看模板Review the template

本快速入门中使用的模板来自 Azure 快速启动模板The template used in this quickstart is from Azure Quickstart Templates. 它创建新的应用程序配置存储,其中包含两个键值。It creates a new App Configuration store with two key-values inside. 然后,它使用 reference 函数输出这两个键值资源的值。It then uses the reference function to output the values of the two key-value resources. 通过这种方式读取键的值,就能在模板的其他位置使用键值。Reading the key's value in this way allows it to be used in other places in the template.

本快速入门使用 copy 元素来创建多个键值资源实例。The quickstart uses the copy element to create multiple instances of key-value resource. 若要详细了解 copy 元素,请参阅 ARM 模板中的资源迭代To learn more about the copy element, see Resource iteration in ARM templates.

重要

此模板需要的应用程序配置资源提供程序版本为 2020-07-01-preview 或更高版本。This template requires App Configuration resource provider version 2020-07-01-preview or later. 此版本使用 reference 函数来读取键值。This version uses the reference function to read key-values. 2020-07-01-preview 版本开始,将不再提供以前的版本中用于读取键值的 listKeyValue 函数。The listKeyValue function that was used to read key-values in the previous version is not available starting in version 2020-07-01-preview.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "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": "2020-07-01-preview",
      "name": "[parameters('configStoreName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "standard"
      }
    },
    {
      "type": "Microsoft.AppConfiguration/configurationStores/keyValues",
      "apiVersion": "2020-07-01-preview",
      "name": "[concat(parameters('configStoreName'), '/', parameters('keyValueNames')[copyIndex()])]",
      "copy": {
        "name": "keyValueCopy",
        "count": "[length(parameters('keyValueNames'))]"
      },
      "dependsOn": [
        "[parameters('configStoreName')]"
      ],
      "properties": {
        "value": "[parameters('keyValueValues')[copyIndex()]]",
        "contentType": "[parameters('contentType')]",
        "tags": "[parameters('tags')]"
      }
    }
  ],
  "outputs": {
    "reference-key-value-value": {
      "value": "[reference(resourceId('Microsoft.AppConfiguration/configurationStores/keyValues', parameters('configStoreName'), parameters('keyValueNames')[0]), '2020-07-01-preview').value]",
      "type": "string"
    },
    "reference-key-value-object": {
      "value": "[reference(resourceId('Microsoft.AppConfiguration/configurationStores/keyValues', parameters('configStoreName'), parameters('keyValueNames')[1]), '2020-07-01-preview')]",
      "type": "object"
    }
  }
}

该模板中定义了两个 Azure 资源:Two Azure resources are defined in the template:

提示

keyValues 资源的名称是键和标签的组合。The keyValues resource's name is a combination of key and label. 键和标签由 $ 分隔符联接。The key and label are joined by the $ delimiter. 标签是可选的。The label is optional. 在上面的示例中,名称为 myKeykeyValues 资源创建了一个不带标签的键值。In the above example, the keyValues resource with name myKey creates a key-value without a label.

使用百分号编码(也称为 URL 编码),就能在键或标签中包含 ARM 模板资源名称中不允许使用的字符。Percent-encoding, also known as URL encoding, allows keys or labels to include characters that are not allowed in ARM template resource names. % 也不是允许使用的字符,因此在其位置使用了 ~% is not an allowed character either, so ~ is used in its place. 若要正确对名称编码,请按照以下步骤操作:To correctly encode a name, follow these steps:

  1. 应用 URL 编码Apply URL encoding
  2. ~ 替换为 ~7EReplace ~ with ~7E
  3. % 替换为 ~Replace % with ~

例如,若要创建键名为 AppName:DbEndpoint 且标签名为 Test 的键值对,资源名应为 AppName~3ADbEndpoint$TestFor example, to create a key-value pair with key name AppName:DbEndpoint and label name Test, the resource name should be AppName~3ADbEndpoint$Test.

备注

借助应用程序配置,可通过虚拟网络上的专用链接访问键值数据。App Configuration allows key-value data access over a private link from your virtual network. 默认情况下,启用此功能后,通过公用网络对你的应用程序配置数据的所有请求都会被拒绝。By default, when the feature is enabled, all requests for your App Configuration data over the public network are denied. ARM 模板在你的虚拟网络外部运行,因此不允许从 ARM 模板访问数据。Because the ARM template runs outside your virtual network, data access from an ARM template isn't allowed. 若要在使用专用链接时允许从 ARM 模板访问数据,可使用以下 Azure CLI 命令启用公共网络访问。To allow data access from an ARM template when a private link is used, you can enable public network access by using the following Azure CLI command. 在这种情况下,请务必考虑启用公用网络访问的安全隐患。It's important to consider the security implications of enabling public network access in this scenario.

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

部署模板Deploy the template

选择下图登录到 Azure 并打开一个模板。Select the following image to sign in to Azure and open a template. 该模板创建应用程序配置存储,其中包含两个键值。The template creates an App Configuration store with two key-values inside.

“部署到 Azure”Deploy to Azure

你也可以使用以下 PowerShell cmdlet 部署模板。You can also deploy the template by using the following PowerShell cmdlet. 键值将位于 PowerShell 控制台的输出中。The key-values will be in the output of PowerShell console.

$projectName = Read-Host -Prompt "Enter a project name that is used for generating resource names"
$location = Read-Host -Prompt "Enter the location (i.e. chinanorth)"
$templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-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 ..."

查看已部署的资源Review deployed resources

  1. 登录到 Azure 门户Sign in to the Azure portal.
  2. 在 Azure 门户搜索框中,键入“应用程序配置”。In the Azure portal search box, type App Configuration. 从列表中选择“应用程序配置”。Select App Configuration from the list.
  3. 选择新创建的应用程序配置资源。Select the newly created App Configuration resource.
  4. 在“操作”下,单击“配置资源管理器” 。Under Operations, click Configuration explorer.
  5. 验证有两个键值。Verify that two key-values exist.

清理资源Clean up resources

如果不再需要本教程中所述的资源组、应用程序配置存储和所有相关资源,请将其删除。When no longer needed, delete the resource group, the App Configuration store, and all related resources. 如果将来还打算使用该应用程序配置存储,可以跳过删除它的步骤。If you're planning to use the App Configuration store in the future, you can skip deleting it. 如果不打算继续使用该存储,请运行以下 cmdlet,删除本快速入门中创建的所有资源:If you aren't going to continue to use this store, delete all resources created by this quickstart by running the following cmdlet:

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

后续步骤Next steps

若要了解如何创建使用 Azure 应用程序配置的其他应用程序,请继续阅读以下文章:To learn about creating other applications with Azure App Configuration, continue to the following article: