Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this Quickstart, you use Visual Studio Code to build an app that responds to database changes in a No SQL database in Azure Cosmos DB. After testing the code locally, you deploy it to a new serverless function app you create running in a Flex Consumption plan in Azure Functions.
The project source uses the Azure Developer CLI (azd) extension with Visual Studio Code to simplify initializing and verifying your project code locally, as well as and deploying your code to Azure. This deployment follows current best practices for secure and scalable Azure Functions deployments.
Important
This article is currently only supported in C#, Python, and TypeScript. To complete the quickstart, select one of these supported languages at the top of the article.
Prerequisites
An Azure account with an active subscription. Create an account for free.
Visual Studio Code on one of the supported platforms.
The Azure Functions extension for Visual Studio Code. This extension requires Azure Functions Core Tools. When this tool isn't available locally, the extension tries to install it by using a package-based installer. You can also install or update the Core Tools package by running
Azure Functions: Install or Update Azure Functions Core Toolsfrom the command palette. If you don't have npm or Homebrew installed on your local computer, you must instead manually install or update Core Tools.
C# extension for Visual Studio Code.
- Node.js 18.x or above. Use the
node --versioncommand to check your version.
Python versions that are supported by Azure Functions. For more information, see How to install Python.
The Python extension for Visual Studio Code.
- The Azure Developer CLI extension for Visual Studio Code.
Initialize the project
You can use the azd init command from the command palette to create a local Azure Functions code project from a template.
In Visual Studio Code, open a folder or workspace in which you want to create your project.
Press F1 to open the command palette, search for and run the command
Azure Developer CLI (azd): Initialize App (init), and then choose Select a template.There might be a slight delay while
azdinitializes the current folder or workspace.
When prompted, choose Select a template, then search for and select
Azure Functions with Cosmos DB Bindings (.NET).When prompted, enter a unique environment name, such as
cosmosdbchanges-dotnet.This command pulls the project files from the template repository and initializes the project in the current folder or workspace. In
azd, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also part of the name of the resource group you create in Azure.
When prompted, choose Select a template, then search for and select
Azure Functions TypeScript CosmosDB trigger.When prompted, enter a unique environment name, such as
cosmosdbchanges-ts.This command pulls the project files from the template repository and initializes the project in the current folder or workspace. In
azd, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also part of the name of the resource group you create in Azure.
When prompted, choose Select a template, then search for and select
Azure Functions Python with CosmosDB triggers and bindings....When prompted, enter a unique environment name, such as
cosmosdbchanges-py.This command pulls the project files from the template repository and initializes the project in the current folder or workspace. In
azd, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also part of the name of the resource group you create in Azure.
Run this command, depending on your local operating system, to grant configuration scripts the required permissions:
Run this command with sufficient privileges:
chmod +x ./infra/scripts/*.sh
Before you can run your app locally, you must create the resources in Azure. This project doesn't use local emulation for Azure Cosmos DB.
Create Azure resources
This project is configured to use the azd provision command to create a function app in a Flex Consumption plan, along with other required Azure resources that follows current best practices.
In Visual Studio Code, press F1 to open the command palette, search for and run the command
Azure Developer CLI (azd): Sign In with Azure Developer CLI, and then sign in using your Azure account.Press F1 to open the command palette, search for and run the command
Azure Developer CLI (azd): Provision Azure resources (provision)to create the required Azure resources:When prompted in the Terminal window, provide these required deployment parameters:
Prompt Description Select an Azure Subscription to use Choose the subscription in which you want your resources to be created. location deployment parameter Azure region in which to create the resource group that contains the new Azure resources. Only regions that currently support the Flex Consumption plan are shown. vnetEnabled deployment parameter While the template supports creating resources inside a virtual network, to simplify deployment and testing, choose False.The
azd provisioncommand uses your response to these prompts with the Bicep configuration files to create and configure these required Azure resources, following the latest best practices:- Flex Consumption plan and function app
- Azure Cosmos DB account
- Azure Storage (required) and Application Insights (recommended)
- Access policies and roles for your account
- Service-to-service connections using managed identities (instead of stored connection strings)
Post-provision hooks also generate the local.settings.json file required when running locally. This file also contains the settings required to connect to your Azure Cosmos DB database in Azure.
Tip
Should any steps fail during provisioning, you can rerun the
azd provisioncommand again after resolving any issues.After the command completes successfully, you can run your project code locally and trigger on the Azure Cosmos DB database in Azure.
Run the function locally
Visual Studio Code integrates with Azure Functions Core tools to let you run this project on your local development computer before you publish to your new function app in Azure.
Press F1 and in the command palette search for and run the command
Azurite: Start.To start the function locally, press F5 or the Run and Debug icon in the left-hand side Activity bar. The Terminal panel displays the output from Core Tools. Your app starts in the Terminal panel, and you can see the name of the function that's running locally.
If you have trouble running on Windows, make sure that the default terminal for Visual Studio Code isn't set to WSL Bash.
With Core Tools still running in Terminal, press F1 and in the command palette search for and run the command
NoSQL: Create Item...and select both thedocument-dbdatabase and thedocumentscontainer.Replace the contents of the New Item.json file with this JSON data and select Save:
{ "id": "doc1", "title": "Sample document", "content": "This is a sample document for testing my Azure Cosmos DB trigger in Azure Functions." }After you select Save, you see the execution of the function in the terminal and the local document is updated to include metadata added by the service.
When you're done, press Ctrl+C in the terminal window to stop the
func.exehost process.
Review the code (optional)
The function is triggered based on the change feed in an Azure Cosmos DB NoSQL database. These environment variables configure how the trigger monitors the change feed:
COSMOS_CONNECTION__accountEndpoint: The Cosmos DB account endpointCOSMOS_DATABASE_NAME: The name of the database to monitorCOSMOS_CONTAINER_NAME: The name of the container to monitor
These environment variables are created for you both in Azure (function app settings) and locally (local.settings.json) during the azd provision operation.
You can review the code that defines the Azure Cosmos DB trigger in the CosmosTrigger.cs project file.
You can review the code that defines the Azure Cosmos DB trigger in the cosmos_trigger.ts project file.
You can review the code that defines the Azure Cosmos DB trigger in the function_app.py project file.
After you review and verify your function code locally, it's time to publish the project to Azure.
Deploy to Azure
You can run the azd deploy command from Visual Studio Code to deploy the project code to your already provisioned resources in Azure.
Press F1 to open the command palette, search for and run the command
Azure Developer CLI (azd): Deploy to Azure (deploy).The
azd deploycommand packages and deploys your code to the deployment container. The app is then started and runs in the deployed package.After the command completes successfully, your app is running in Azure.
Invoke the function on Azure
In Visual Studio Code, press F1 and in the command palette search for and run the command
Azure: Open in portal, selectFunction app, and choose your new app. Sign in with your Azure account, if necessary.This command opens your new function app in the Azure portal.
In the Overview tab on the main page, select your function app name and then the Logs tab.
Use the
NoSQL: Create Itemcommand in Visual Studio Code to again add a document to the container as before.Verify again that the function gets triggered by an update in the monitored container.
Redeploy your code
You can run the azd deploy command as many times as you need to deploy code updates to your function app.
Note
Deployed code files are always overwritten by the latest deployment package.
Your initial responses to azd prompts and any environment variables generated by azd are stored locally in your named environment. Use the azd env get-values command to review all of the variables in your environment that were used when creating Azure resources.
Clean up resources
When you're done working with your function app and related resources, you can use this command to delete the function app and its related resources from Azure and avoid incurring any further costs:
azd down --no-prompt
Note
The --no-prompt option instructs azd to delete your resource group without a confirmation from you.
This command doesn't affect your local code project.