Project: YugabyteDB (App Definition and Development / Database)
File: azure-functions.md

title: How to Develop Azure Functions with YugabyteDB headerTitle: Develop Azure Functions linkTitle: Azure Functions description: How to Develop Azure Functions with YugabyteDB image: /images/tutorials/azure/icons/Function-App-Icon.svg headcontent: Use YugabyteDB as the backend for Azure Functions menu: preview_tutorials: identifier: tutorials-azure-functions parent: tutorials-azure weight: 20 type: docs In this tutorial, we'll guide you through the steps required to develop and deploy a serverless function using Azure Functions and YugabyteDB. Serverless functions serve many use cases, including API endpoints, scheduled jobs, and file processing. Azure Functions work with a number of , which allow developers to define precisely when a function will be invoked and how it will interact with other services. In the following sections, you will: Cover the prerequisites for developing an Azure Function backed by our fully managed DBaaS, . Deploy a database cluster to Azure on YugabyteDB Managed. Develop an Azure Function using an HTTP trigger. Deploy this serverless function to Azure. Let's begin by installing the dependencies required to begin effectively developing Azure Functions. First, visit GitHub for the we will be deploying to Azure. We'll develop and deploy an HTTP trigger function, which connects to YugabyteDB and returns the current inventory of our shoe store, YB Shoes. A YugabyteDB Managed account. Sign up for a . A subscription, with a resource group and storage account Access to the resource v18+ For steps on creating a cluster in YugabyteDB Managed, see the . For a configuration that provides fault tolerance across availability zones, deploy a on Azure in the westus3 region. However, you can start with an always-free single-node . Add your computer's IP address to the cluster so that you can run your serverless functions locally in development. Now that we have a working cluster in YugabyteDB Managed, let's add some data. Now that our cluster is running in the cloud, we can seed it with data using the provided `schema.sql` and `data.sql` files. Use the to connect to your cluster. Execute the commands in the `schema.sql` script against your cluster. Execute the commands in the `data.sql` script against your cluster. With your cluster seeded with data, it's time to build the serverless function to connect to it. The Azure Functions Core Tools provide a command-line interface for developing functions on your local machine and deploying them to Azure. Initialize a new Azure Functions project. ```sh func init YBAzureFunctions --worker-runtime javascript --model V4 ``` Create a new HTTP trigger

```sh cd YBAzureFunctions func new --template "Http Trigger" --name GetShoeInventory ``` Install the YugabyteDB node-postgres Smart Driver. ```sh npm install @yugabytedb/pg ``` Update the boilerplate code in GetShoeInventory.js. ```javascript const { app } = require("@azure/functions"); const { Client } = require("pg"); app.http("GetShoeInventory", { methods: ["GET"], authLevel: "anonymous", handler: async () => { // Read the PostgreSQL connection settings from local.settings.json console.log("process.env.DBHOST:", process.env.DBHOST); const client = new Client({ user: process.env.DB_USERNAME, host: process.env.DB_HOST, database: "yugabyte", password: process.env.DB_PASSWORD, port: 5433, max: 10, idleTimeoutMillis: 0, ssl: { rejectUnauthorized: true, ca: atob(process.env.DB_CERTIFICATE), servername: process.env.DB_HOST, }, }); try { // Connect to the PostgreSQL database await client.connect(); // Query YugabyteDB for shoe inventory const query = "SELECT i.quantity, s.model, s.brand from inventory i INNER JOIN shoes s on i.shoe_id = s.id;"; const result = await client.query(query); // Process the query result const data = result.rows; // Close the database connection await client.end(); return { status: 200, body: JSON.stringify(data), }; } catch (error) { console.error("Error connecting to the database:", error); return { status: 500, body: "Internal Server Error", }; } }, }); ``` Update local.settings.json with the configuration settings required to run the GetShoeInventory function locally. ```conf local.settings.json ... "DB_USERNAME": "admin", "DBPASSWORD": [YUGABYTEDB_PASSWORD], "DBHOST": [YUGABYTEDB_HOST], "DB_NAME": "yugabyte", "DBCERTIFICATE": [BASE64ENCODEDYUGABYTEDBCERTIFICATE] ``` Run the function locally. ```sh func start ``` Test your function in the browser at <http://localhost:7071/api/GetShoeInventory>. Now we'll deploy our function to Azure. We can deploy our application to Azure using the . Create a Function App. ```sh az functionapp create --resource-group RESOURCEGROUPNAME --consumption-plan-location eastus2 --runtime node --runtime-version 18 --functions-version 4 --name YBAzureFunctions --storage-account STORAGEACCOUNTNAME ``` Get the and add them to your YugabyteDB cluster's . This ensures that a connection can be made between Azure Functions and YugabyteDB. ```sh az functionapp show --resource-group RESOURCEGROUPNAME --name YBAzureFunctions --query possibleOutboundIpAddresses --output tsv ``` You can also obtain these addresses in the Networking tab of the Azure portal. Configure the application settings. ```sh az functionapp config appsettings set -g RESOURCEGROUPNAME -n YBAzureFunctions --setting DBHOST=[YUGABYTEDBHOST] DBUSERNAME=admin DBPASSWORD=[YUGABYTEDBPASSWORD] DBCERTIFICATE=[BASE64ENCODEDYUGABYTEDB_CERTIFICATE] ``` Publish Function App to Azure. ```sh func azure functionapp publish YBAzureFunctions ``` Verify that the function was published successfully. ```sh curl https://ybazurefunctions.azurewebsites.net/api/GetShoeInventory ``` ```output.json [{"quantity":24,"model":"speedgoat 5","brand":"hoka one one"},{"quantity":74,"model":"adizero adios pro 3","brand":"adidas"},{"quantity":13,"model":"torrent 2","brand":"hoka one one"},{"quantity":99,"model":"vaporfly 3","brand":"nike"}] ``` As you can see, it's easy to begin developing and publishing database-backed Azure Functions with YugabyteDB. If you're also interested in building a Node.js web application for YB Shoes using Azure App Service and YugabyteDB, check out the blog .