创建用于连接到 Azure Cosmos DB 的 Azure FunctionCreate an Azure Function that connects to an Azure Cosmos DB

此 Azure Functions 示例脚本先创建一个函数应用,然后将该函数连接到 Azure Cosmos DB 数据库。This Azure Functions sample script creates a function app and connects the function to an Azure Cosmos DB database. 创建的应用设置(包含连接)可以与 Azure Cosmos DB 触发器或绑定配合使用。The created app setting that contains the connection can be used with an Azure Cosmos DB trigger or binding.

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

如果在本地使用 CLI,请确保运行 Azure CLI 2.0 或更高版本。If you use the CLI locally, make sure that you are running the Azure CLI version 2.0 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

此示例创建 Azure Function app,并将 Cosmos DB 终结点和访问密钥添加到应用设置。This sample creates an Azure Function app and adds a Cosmos DB endpoint and access key to app settings.

#!/bin/bash

# create a resource group with location
az group create `
  --name myResourceGroup `
  --location chinanorth

# create a storage account 
az storage account create `
  --name funccosmosdbstore `
  --location chinanorth `
  --resource-group myResourceGroup `
  --sku Standard_LRS

# create an app service plan
az appservice plan create `
  --name myappserviceplan `
  --resource-group myResourceGroup `
  --location chinanorth

# create a new function app, assign it to the resource group you have just created
az functionapp create `
  --name myfunccosmosdb `
  --resource-group myResourceGroup `
  --storage-account funccosmosdbstore `
  --plan myappserviceplan

# create cosmosdb database, name must be lowercase.
az cosmosdb create `
  --name myfunccosmosdb `
  --resource-group myResourceGroup

# Retrieve cosmosdb connection string
$endpoint=az cosmosdb show --name myfunccosmosdb --resource-group myResourceGroup --query documentEndpoint --output tsv

$key=az cosmosdb list-keys --name myfunccosmosdb --resource-group myResourceGroup --query primaryMasterKey --output tsv

# configure function app settings to use cosmosdb connection string
az functionapp config appsettings set `
  --name myfunccosmosdb `
  --resource-group myResourceGroup `
  --setting CosmosDB_Endpoint=$endpoint CosmosDB_Key=$key

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令:表中的每条命令均链接到特定于命令的文档。This script uses the following commands: Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 使用相关位置创建资源组Create a resource group with location
az storage accounts createaz storage accounts create 创建存储帐户Create a storage account
az functionapp createaz functionapp create 在无服务器消耗计划中创建函数应用。Creates a function app in the serverless consumption plan.
az cosmosdb createaz cosmosdb create 创建 Azure Cosmos DB 数据库。Create an Azure Cosmos DB database.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Functions 文档中找到其他 Azure Functions CLI 脚本示例。Additional Azure Functions CLI script samples can be found in the Azure Functions documentation.