Edit

Share via


Quickstart: Respond to Azure SQL Database changes using Azure Functions

In this Quickstart, you use Visual Studio Code to build an app that responds to changes in an Azure SQL Database table. After testing the code locally, you deploy it to a new serverless function app 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, 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

  • Node.js 18.x or above. Use the node --version command to check your version.

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.

  1. In Visual Studio Code, open a folder or workspace in which you want to create your project.

  2. 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.

  3. When prompted, search for and select Azure Functions with SQL Triggers and Bindings.

  4. When prompted, enter a unique environment name, such as sqldbchanges.

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.

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.

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.

Before you can run your app locally, you must create the resources in Azure.

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 follow current best practices.

  1. 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.

  2. 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.

  3. When prompted in the Terminal window, provide these required deployment parameters:

    Prompt Description
    Select an Azure Subscription to use Select 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 provision command 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 SQL Database (default name: ToDo)
  • 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, which is required to run locally. This file contains the settings required to connect to your database in Azure.

Review the code (optional)

The sample defines two functions:

Function name Code file Trigger type Description
httptrigger-sql-output sql_output_http_trigger.cs HTTP trigger Accepts a properly formatted JSON payload and uses the SQL output binding to insert the object as a row in the ToDo table.
ToDoTrigger sql_trigger.cs SQL trigger Listens on the ToDo table for row-level changes and returns an object that represents the changed row.

The ToDoItem type is defined in ToDoItem.cs.

Function name Code file Trigger type Description
http_trigger_sql_output function_app.py HTTP trigger Accepts a properly formatted JSON payload and uses the SQL output binding to insert the object as a row in the ToDo table.
httptrigger-sql-output sql_trigger_todo SQL trigger Listens on the ToDo table for row-level changes and returns an object that represents the changed row.

The ToDoItem type is defined in todo_item.py.

Function name Code file Trigger type Description
httpTriggerSqlOutput sql_output_http_trigger.ts HTTP trigger Accepts a properly formatted JSON payload and uses the SQL output binding to insert the object as a row in the ToDo table.
sqlTriggerToDo sql_trigger.ts SQL trigger Listens on the ToDo table for row-level changes and returns an object that represents the changed row.

The ToDoItem type is defined in ToDoItem.ts.

Both functions use the app-level AZURE_SQL_CONNECTION_STRING_KEY_* environment variables that define an identity-based connection to the Azure SQL Database instance using Microsoft Entra ID authentication. These environment variables are created for you both in Azure (function app settings) and locally (local.settings.json) during the azd provision operation.

Connect to the SQL database

You can use the SQL Server (mssql) extension for Visual Studio Code to connect to the new database. This extension helps you make updates in the ToDo table to run the SQL trigger function.

  1. Press F1 and in the command palette search for and run the command MS SQL: Add Connection.

  2. In the Connection dialog, change Input type to Browse Azure and then set these remaining options:

    Option Choose Description
    Server Your SQL Server instance By default, all servers accessible to your Azure account are displayed. Use Subscription, Resource group, and Location to help filter the servers list.
    Database ToDo The database created during the provisioning process.
    Authentication type Microsoft Entra ID If you aren't already signed-in, select Sign in and sign in to your Azure account.
    Tenant ID The specific account tenant. If your account has more than one tenant, choose the correct tenant for your subscription.
  3. Select Connect to connect to your database. The connection uses your local user account, which is granted admin permissions in the hosting server and mapped to dbo in the database.

  4. In the SQL Server view, locate and expand Connections and then your new server in SQL Server explorer. Expand Tables and verify that the ToDo table exists. If it doesn't exist, you might need run azd provision again and check for errors.

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.

  1. Press F1 and in the command palette search for and run the command Azurite: Start.

  2. 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.

With the app running, you can verify and debug both function triggers.

To verify the HTTP trigger function that writes to a SQL output binding:

  1. Copy this JSON object, which you can also find in the test.http project file:

    {
      "id": "11111111-1111-1111-1111-111111111111",
      "order": 1,
      "title": "Test Todo Item",
      "url": "https://example.com",
      "completed": false
    }
    

    This data represents a row that you insert in your SQL database when you call the HTTP endpoint. The output binding translates the data object into an INSERT operation in the database.

  2. With the app running, in the Azure view under Workspace expand Local project > Functions.

  3. Right-select your HTTP function (or Ctrl+click on macOS), select Execute function now, paste the copied JSON data, and press Enter.

    The function handles the HTTP request and writes the item to the connected SQL database and returns the created object.

  4. Back in the SQL Server explorer, right-select the ToDo table (or Ctrl+click on macOS), and choose Select Top 1000. When the query executes, it returns the inserted or updated row.

  5. Repeat Step 3 and resend the same data object with the same ID. This time, the output binding performs an UPDATE operation instead of an INSERT and modifies the existing row in the database.

When you're done, type Ctrl+C in the terminal to stop the Core Tools process.

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.

  1. Press F1 to open the command palette, search for and run the command Azure Developer CLI (azd): Deploy to Azure (deploy).

    The azd deploy command packages and deploys your code to the deployment container. The app is then started and runs in the deployed package.

  2. After the command completes successfully, your app is running in Azure. Make a note of the Endpoint value, which is the URL of your function app running in Azure.

Invoke the function on Azure

  1. In Visual Studio Code, press F1 and in the command palette search for and run the command Azure: Open in portal, select Function app, and choose your new app. Sign in with your Azure account, if necessary.

  2. Select Log stream in the left pane, which connects to the Application Insights logs for your app.

  3. Return to Visual Studio Code to run both the functions in Azure.

  1. Press F1 to open the command palette, search for and run the command Azure Functions: Execute Function Now....

  2. Search for and select your remote function app from the list, then select the HTTP trigger function.

  3. As before, paste your JSON object data in Enter payload body and press Enter.

    {
      "id": "11111111-1111-1111-1111-111111111111",
      "order": 1,
      "title": "Test Todo Item",
      "url": "https://example.com",
      "completed": false
    }
    

    To perform an INSERT instead of an UPDATE, replace the id with a new GUID value.

  4. Return to the portal and view the execution output in the log window.

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.