Enabling serverless computing with Azure Cosmos DB's integrated functions
TypeScriptTo enable serverless computing with Azure Cosmos DB and use its integrated functions, you'll use Azure Functions, which can be triggered by various sources, including a Cosmos DB change feed. This allows you to perform tasks in response to database events. With Pulumi, you can define both the Azure Cosmos DB instance and the Azure Functions app within your infrastructure code.
Below is a Pulumi program in TypeScript that sets up an Azure Cosmos DB account, with a SQL API database and container. It also includes an Azure Function App that's triggered whenever a new document is added to the Cosmos DB container. The Azure Function could contain logic to process the newly inserted document.
Firstly, ensure you have Pulumi installed and configured for use with Azure. This involves logging in to Pulumi and setting up the Azure CLI with credentials to access your Azure account.
The following Pulumi program will perform these main steps:
- Create a new resource group to contain all resources.
- Provision an Azure Cosmos DB account using the SQL (DocumentDB) API.
- Add a database and a container in the Cosmos DB account.
- Deploy an Azure Functions app with a function triggered by Cosmos DB.
- Grant the necessary permissions to the Function App so it can read from the specified Cosmos DB container.
Here's the TypeScript program:
import * as pulumi from "@pulumi/pulumi"; import * as azure from "@pulumi/azure"; import * as azure_native from "@pulumi/azure-native"; // Create an Azure Resource Group const resourceGroup = new azure.core.ResourceGroup("my-resource-group"); // Create an Azure Cosmos DB account const cosmosDbAccount = new azure.cosmosdb.Account("cosmosDbAccount", { resourceGroupName: resourceGroup.name, // Serverless capacity mode is the correct choice for serverless computing enableServerless: true, geoLocations: [ { location: resourceGroup.location, failoverPriority: 0, } ], consistencyPolicy: { consistencyLevel: "Session", }, }); // Add a SQL database inside the Cosmos DB account const database = new azure.cosmosdb.SqlDatabase("sqlDatabase", { resourceGroupName: resourceGroup.name, accountName: cosmosDbAccount.name, throughput: 400, }); // Add a container inside the SQL database const container = new azure.cosmosdb.SqlContainer("sqlContainer", { resourceGroupName: resourceGroup.name, accountName: cosmosDbAccount.name, databaseName: database.name, partitionKeyPath: "/id", throughput: 400, }); // Create an Azure App Service Plan for the Function App const appServicePlan = new azure.appservice.Plan("appServicePlan", { resourceGroupName: resourceGroup.name, kind: "FunctionApp", sku: { tier: "Dynamic", size: "Y1", }, }); // Create the Function App const functionApp = new azure.appservice.FunctionApp("myFunctionApp", { resourceGroupName: resourceGroup.name, appServicePlanId: appServicePlan.id, appSettings: { // Set the connection string to the Cosmos DB account "AzureWebJobsCosmosDBConnectionString": cosmosDbAccount.connectionStrings.apply(cs => cs[0]), }, // Configuration values for the Function App to integrate with the Cosmos DB trigger functions: [ { name: "CosmosDBTriggerFunction", config: { bindings: [ { type: "cosmosDBTrigger", name: "documents", direction: "in", leaseCollectionName: "leases", connectionStringSetting: "AzureWebJobsCosmosDBConnectionString", databaseName: database.name, collectionName: container.name, createLeaseCollectionIfNotExists: true, }, ], disabled: false, }, }, ], }); // Print out the endpoint of the Function App export const endpoint = pulumi.interpolate`https://${functionApp.defaultHostname}/api`;
To explain the above Pulumi TypeScript program:
- We start by creating a new Resource Group, which is a logical container where all Azure resources reside.
- We provision a Cosmos DB account tailored for serverless workloads by setting enableServerless to true.
- We then create a SQL API database and a container inside the Cosmos DB account. In Cosmos DB, containers are similar to tables or collections in other database systems.
- We establish an App Service Plan with a consumption plan (serverless) for hosting our Function App.
- Finally, we deploy a Function App with a specific function configured to be triggered by Cosmos DB changes. The function is set up with the necessary information about the Cosmos DB instance it should monitor, including the database and container names, and we ensure that a lease collection for managing the function's state is created if it does not exist.
This function will then be run every time there is a change in the specified container. You would supply the function code that defines what actions to take when the function is triggered.
Remember that this code would need to be part of a Pulumi project. If you wanted to deploy this infrastructure, you'd write the function's code, place it in the appropriate directory structure expected by Azure Functions, and run
pulumi up
to provision the resources.