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 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
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.
- The SQL Server (mssql) 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.When prompted, search for and select
Azure Functions with SQL Triggers and Bindings.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.
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 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.
Press F1 and in the command palette search for and run the command
MS SQL: Add Connection.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 ToDoThe 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. 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
dboin the database.In the SQL Server view, locate and expand Connections and then your new server in SQL Server explorer. Expand Tables and verify that the
ToDotable exists. If it doesn't exist, you might need runazd provisionagain 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.
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.
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:
Copy this JSON object, which you can also find in the
test.httpproject 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
INSERToperation in the database.With the app running, in the Azure view under Workspace expand Local project > Functions.
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.
Back in the SQL Server explorer, right-select the
ToDotable (or Ctrl+click on macOS), and choose Select Top 1000. When the query executes, it returns the inserted or updated row.Repeat Step 3 and resend the same data object with the same ID. This time, the output binding performs an
UPDATEoperation instead of anINSERTand 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.
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. Make a note of the
Endpointvalue, which is the URL of your function app 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.Select Log stream in the left pane, which connects to the Application Insights logs for your app.
Return to Visual Studio Code to run both the functions in Azure.
Press F1 to open the command palette, search for and run the command
Azure Functions: Execute Function Now....Search for and select your remote function app from the list, then select the HTTP trigger function.
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
INSERTinstead of anUPDATE, replace theidwith a new GUID value.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.