您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

创建用于连接到 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 free account before you begin.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 通过 Cloud Shell 可以将 bashPowerShell 与 Azure 服务配合使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To launch Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 选择“试用” 不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell” 按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上方菜单栏中的“Cloud Shell” 按钮。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Launch Cloud Shell.
  2. 选择代码块上的“复制”按钮 以复制代码。Select the Copy button on a code block to copy the code.
  3. 在 Windows 和 Linux 上使用 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上使用 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. Enter 运行此代码。Press Enter to run the code.

如果在本地使用 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

# Function app and storage account names must be unique.
storageName=mystorageaccount$RANDOM
functionAppName="myfuncwithcosmosdb$RANDOM"

# Create a resource group with location.
az group create \
  --name myResourceGroup \
  --location westeurope

# Create a storage account for the function app. 
az storage account create \
  --name $storageName \
  --location westeurope \
  --resource-group myResourceGroup \
  --sku Standard_LRS

# Create a serverless function app in the resource group.
az functionapp create \
  --name $functionAppName \
  --resource-group myResourceGroup \
  --storage-account $storageName \
  --consumption-plan-location westeurope

# Create an Azure Cosmos DB database using the same function app name.
az cosmosdb create \
  --name $functionAppName \
  --resource-group myResourceGroup

# Get the Azure Cosmos DB connection string.
endpoint=$(az cosmosdb show \
  --name $functionAppName \
  --resource-group myResourceGroup \
  --query documentEndpoint \
  --output tsv)

key=$(az cosmosdb list-keys \
  --name $functionAppName \
  --resource-group myResourceGroup \
  --query primaryMasterKey \
  --output tsv)

# Configure function app settings to use the Azure Cosmos DB connection string.
az functionapp config appsettings set \
  --name $functionAppName \
  --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.