Develop Azure Functions by using Visual Studio Code

The Azure Functions extension for Visual Studio Code lets you locally develop functions and deploy them to Azure. If this experience is your first with Azure Functions, you can learn more at An introduction to Azure Functions.

The Azure Functions extension provides these benefits:

  • Edit, build, and run functions on your local development computer.
  • Publish your Azure Functions project directly to Azure.
  • Write your functions in various languages while taking advantage of the benefits of Visual Studio Code.

The extension can be used with the following languages, which are supported by the Azure Functions version 2.x runtime:

*Requires that you set C# script as your default project language.

In this article, examples are currently available only for JavaScript (Node.js) and C# class library functions.

This article provides details about how to use the Azure Functions extension to develop functions and publish them to Azure. Before you read this article, you should create your first function by using Visual Studio Code.

Important

Don't mix local development and portal development for a single function app. When you publish from a local project to a function app, the deployment process overwrites any functions that you developed in the portal.

Prerequisites

Before you install and run the Azure Functions extension, you must meet these requirements:

If you don't have an Azure subscription, create a free account before you begin.

Other resources that you need, like an Azure storage account, are created in your subscription when you publish by using Visual Studio Code.

Important

You can develop functions locally and publish them to Azure without having to start and run them locally. To run your functions locally, you'll need to meet some additional requirements, including an automatic download of Azure Functions Core Tools. To learn more, see Additional requirements for running a project locally.

Install the Azure Functions extension

You can use the Azure Functions extension to create and test functions and deploy them to Azure.

  1. In Visual Studio Code, open Extensions and search for azure functions, or select this link in Visual Studio Code.

  2. Select Install to install the extension for Visual Studio Code:

    Install the extension for Azure Functions

  3. Restart Visual Studio Code and select the Azure icon on the Activity bar. You should see an Azure Functions area in the Side Bar.

    Azure Functions area in the Side Bar

Create an Azure Functions project

The Functions extension lets you create a function app project, along with your first function. The following steps show how to create an HTTP-triggered function in a new Functions project. HTTP trigger is the simplest function trigger template to demonstrate.

  1. From Azure: Functions, select the Create Function icon:

    Create a function

  2. Select the folder for your function app project, and then Select a language for your function project.

  3. Select the HTTP trigger function template, or you can select Skip for now to create a project without a function. You can always add a function to your project later.

    Choose the HTTP trigger template

  4. Type HTTPTrigger for the function name and select Enter, and then select Function authorization. This authorization level requires you to provide a function key when you call the function endpoint.

    Select Function authorization

    A function is created in your chosen language and in the template for an HTTP-triggered function.

    HTTP-triggered function template in Visual Studio Code

The project template creates a project in your chosen language and installs required dependencies. For any language, the new project has these files:

  • host.json: Lets you configure the Functions host. These settings apply when you're running functions locally and when you're running them in Azure. For more information, see host.json reference.

  • local.settings.json: Maintains settings used when you're running functions locally. These settings are used only when you're running functions locally. For more information, see Local settings file.

    Important

    Because the local.settings.json file can contain secrets, you need to exclude it from your project source control.

At this point, you can add input and output bindings to your function by modifying the function.json file or by adding a parameter to a C# class library function.

You can also add a new function to your project.

Install binding extensions

Except for HTTP and timer triggers, bindings are implemented in extension packages. You must install the extension packages for the triggers and bindings that need them. The process for installing binding extensions depends on your project's language.

The easiest way to install binding extensions is to enable extension bundles. When you enable bundles, a predefined set of extension packages is automatically installed.

To enable extension bundles, open the host.json file and update its contents to match the following code:

{
    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    }
}

Add a function to your project

You can add a new function to an existing project by using one of the predefined Functions trigger templates. To add a new function trigger, select F1 to open the command palette, and then search for and run the command Azure Functions: Create Function. Follow the prompts to choose your trigger type and define the required attributes of the trigger. If your trigger requires an access key or connection string to connect to a service, get it ready before you create the function trigger.

The results of this action depend on your project's language:

A new folder is created in the project. The folder contains a new function.json file and the new JavaScript code file.

Add input and output bindings

You can expand your function by adding input and output bindings. The process for adding bindings depends on your project's language. To learn more about bindings, see Azure Functions triggers and bindings concepts.

The following examples connect to a storage queue named outqueue, where the connection string for the storage account is set in the MyStorageConnection application setting in local.settings.json.

Visual Studio Code lets you add bindings to your function.json file by following a convenient set of prompts. To create a binding, right-click (Ctrl+click on macOS) the function.json file in your function folder and select Add binding:

Add a binding to an existing JavaScript function

Following are example prompts to define a new storage output binding:

Prompt Value Description
Select binding direction out The binding is an output binding.
Select binding with direction Azure Queue Storage The binding is an Azure Storage queue binding.
The name used to identify this binding in your code msg Name that identifies the binding parameter referenced in your code.
The queue to which the message will be sent outqueue The name of the queue that the binding writes to. When the queueName doesn't exist, the binding creates it on first use.
Select setting from "local.setting.json" MyStorageConnection The name of an application setting that contains the connection string for the storage account. The AzureWebJobsStorage setting contains the connection string for the storage account you created with the function app.

In this example, the following binding is added to the bindings array in your function.json file:

{
    "type": "queue",
    "direction": "out",
    "name": "msg",
    "queueName": "outqueue",
    "connection": "MyStorageConnection"
}

You can also add the same binding definition directly to your function.json.

In your function code, the msg binding is accessed from the context, as in this example:

context.bindings.msg = "Name passed to the function: " req.query.name;

To learn more, see the Queue storage output binding reference.

The msg parameter is an ICollector<T> type, which represents a collection of messages that are written to an output binding when the function completes. You add one or more messages to the collection. These messages are sent to the queue when the function completes.

To learn more, see the Queue storage output binding documentation.

This table shows the bindings that are supported in the two major versions of the Azure Functions runtime:

Type 1.x 2.x1 Trigger Input Output
Blob storage
Cosmos DB
Event Grid
Event Hubs
HTTP & webhooks
IoT Hub
Microsoft Graph
Excel tables
Microsoft Graph
OneDrive files
Microsoft Graph
Outlook email
Microsoft Graph
events
Microsoft Graph
Auth tokens
Mobile Apps
Notification Hubs
Queue storage
SendGrid
Service Bus
SignalR
Table storage
Timer
Twilio

1 In the version 2.x runtime, all bindings except HTTP and Timer must be registered. See Register binding extensions. All supported 2.x bindings are also supported in the version 3.x, unless otherwise noted.

Publish to Azure

Visual Studio Code lets you publish your Functions project directly to Azure. In the process, you create a function app and related resources in your Azure subscription. The function app provides an execution context for your functions. The project is packaged and deployed to the new function app in your Azure subscription.

When you publish from Visual Studio Code to a new function app in Azure, you are offered both a quick function app create path and an advanced path.

When you publish from Visual Studio Code, you take advantage of the Zip deploy technology.

Quick function app create

When you choose + Create new function app in Azure..., the extension automatically generates values for the Azure resources needed by your function app. These values are based on the function app name that you choose. For an example of using defaults to publish your project to a new function app in Azure, see the Visual Studio Code quickstart article.

If you want to provide explicit names for the created resources, you must choose the advanced create path.

Publish a project to a new function app in Azure by using advanced options

The following steps publish your project to a new function app created with advanced create options:

  1. In the Azure: Functions area, select the Deploy to Function App icon.

    Function app settings

  2. If you're not signed in, you're prompted to Sign in to Azure. You can also Create a free Azure account. After signing in from the browser, go back to Visual Studio Code.

  3. If you have multiple subscriptions, Select a subscription for the function app, and then select + Create New Function App in Azure... Advanced. This Advanced option gives you more control over the resources you create in Azure.

  4. Following the prompts, provide this information:

    Prompt Value Description
    Select function app in Azure Create New Function App in Azure At the next prompt, type a globally unique name that identifies your new function app and then select Enter. Valid characters for a function app name are a-z, 0-9, and -.
    Select an OS Windows The function app runs on Windows.
    Select a hosting plan Consumption plan A serverless Consumption plan hosting is used.
    Select a runtime for your new app Your project language The runtime must match the project that you're publishing.
    Select a resource group for new resources Create New Resource Group At the next prompt, type a resource group name, like myResourceGroup, and then select enter. You can also select an existing resource group.
    Select a storage account Create new storage account At the next prompt, type a globally unique name for the new storage account used by your function app and then select Enter. Storage account names must be between 3 and 24 characters long and can contain only numbers and lowercase letters. You can also select an existing account.
    Select a location for new resources region Select a location in a region near you or near other services that your functions access.

    A notification appears after your function app is created and the deployment package is applied. Select View Output in this notification to view the creation and deployment results, including the Azure resources that you created.

Republish project files

When you set up continuous deployment, your function app in Azure is updated whenever source files are updated in the connected source location. We recommend continuous deployment, but you can also republish your project file updates from Visual Studio Code.

Important

Publishing to an existing function app overwrites the content of that app in Azure.

  1. In Visual Studio Code, select F1 to open the command palette. In the command palette, search for and select Azure Functions: Deploy to function app.

  2. If you're not signed in, you're prompted to Sign in to Azure. After you sign in from the browser, go back to Visual Studio Code. If you have multiple subscriptions, Select a subscription that contains your function app.

  3. Select your existing function app in Azure. When you're warned about overwriting all files in the function app, select Deploy to acknowledge the warning and continue.

The project is rebuilt, repackaged, and uploaded to Azure. The existing project is replaced by the new package, and the function app restarts.

Get the URL of the deployed function

To call an HTTP-triggered function, you need the URL of the function when it's deployed to your function app. This URL includes any required function keys. You can use the extension to get these URLs for your deployed functions.

  1. Select F1 to open the command palette, and then search for and run the command Azure Functions: Copy Function URL.

  2. Follow the prompts to select your function app in Azure and then the specific HTTP trigger that you want to invoke.

The function URL is copied to the clipboard, along with any required keys passed by the code query parameter. Use an HTTP tool to submit POST requests, or a browser for GET requests to the remote function.

Run functions locally

The Azure Functions extension lets you run a Functions project on your local development computer. The local runtime is the same runtime that hosts your function app in Azure. Local settings are read from the local.settings.json file.

Additional requirements for running a project locally

To run your Functions project locally, you must meet these additional requirements:

Configure the project to run locally

The Functions runtime uses an Azure Storage account internally for all trigger types other than HTTP and webhooks. So you need to set the Values.AzureWebJobsStorage key to a valid Azure Storage account connection string.

This section uses the Azure Storage extension for Visual Studio Code with Azure Storage Explorer to connect to and retrieve the storage connection string.

To set the storage account connection string:

  1. In Visual Studio, open Cloud Explorer, expand Storage Account > Your Storage Account, and then select Properties and copy the Primary Connection String value.

  2. In your project, open the local.settings.json file and set the value of the AzureWebJobsStorage key to the connection string you copied.

  3. Repeat the previous step to add unique keys to the Values array for any other connections required by your functions.

For more information, see Local settings file.

Debugging functions locally

To debug your functions, select F5. If you haven't already downloaded Core Tools, you're prompted to do so. When Core Tools is installed and running, output is shown in the Terminal. This is the same as running the func host start Core Tools command from the Terminal, but with additional build tasks and an attached debugger.

When the project is running, you can trigger your functions as you would when the project is deployed to Azure. When the project is running in debug mode, breakpoints are hit in Visual Studio Code, as expected.

The request URL for HTTP triggers is displayed in the output in the Terminal. Function keys for HTTP triggers aren't used when a project is running locally. For more information, see Strategies for testing your code in Azure Functions.

To learn more, see Work with Azure Functions Core Tools.

Local settings file

The local.settings.json file stores app settings, connection strings, and settings used by local development tools. Settings in the local.settings.json file are used only when you're running projects locally. The local settings file has this structure:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "<language worker>",
    "AzureWebJobsStorage": "<connection-string>",
    "AzureWebJobsDashboard": "<connection-string>",
    "MyBindingConnection": "<binding-connection-string>"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*",
    "CORSCredentials": false
  },
  "ConnectionStrings": {
    "SQLConnectionString": "<sqlclient-connection-string>"
  }
}

These settings are supported when you run projects locally:

Setting Description
IsEncrypted When this setting is set to true, all values are encrypted with a local machine key. Used with func settings commands. Default value is false.
Values Array of application settings and connection strings used when a project is running locally. These key-value (string-string) pairs correspond to application settings in your function app in Azure, like AzureWebJobsStorage. Many triggers and bindings have a property that refers to a connection string app setting, like Connection for the Blob storage trigger. For these properties, you need an application setting defined in the Values array.
AzureWebJobsStorage is a required app setting for triggers other than HTTP.
Version 2.x of the Functions runtime requires the [FUNCTIONS_WORKER_RUNTIME] setting, which is generated for your project by Core Tools.
When you have the Azure storage emulator installed locally and you set AzureWebJobsStorage to UseDevelopmentStorage=true, Core Tools uses the emulator. The emulator is useful during development, but you should test with an actual storage connection before deployment.
Values must be strings and not JSON objects or arrays. Setting names can't include a colon (:) or a double underline (__). These characters are reserved by the runtime.
Host Settings in this section customize the Functions host process when you run projects locally. These settings are separate from the host.json settings, which also apply when you run projects in Azure.
LocalHttpPort Sets the default port used when running the local Functions host (func host start and func run). The --port command-line option takes precedence over this setting.
CORS Defines the origins allowed for cross-origin resource sharing (CORS). Origins are supplied as a comma-separated list with no spaces. The wildcard value (*) is supported, which allows requests from any origin.
CORSCredentials When set to true, allows withCredentials requests.
ConnectionStrings A collection. Don't use this collection for the connection strings used by your function bindings. This collection is used only by frameworks that typically get connection strings from the ConnectionStrings section of a configuration file, like Entity Framework. Connection strings in this object are added to the environment with the provider type of System.Data.SqlClient. Items in this collection aren't published to Azure with other app settings. You must explicitly add these values to the Connection strings collection of your function app settings. If you're creating a SqlConnection in your function code, you should store the connection string value with your other connections in Application Settings in the portal.

By default, these settings aren't migrated automatically when the project is published to Azure. After publishing finishes, you're given the option of publishing settings from local.settings.json to your function app in Azure. To learn more, see Publish application settings.

Values in ConnectionStrings are never published.

The function application settings values can also be read in your code as environment variables. For more information, see the Environment variables sections of these language-specific reference articles:

Application settings in Azure

The settings in the local.settings.json file in your project should be the same as the application settings in the function app in Azure. Any settings you add to local.settings.json must also be added to the function app in Azure. These settings aren't uploaded automatically when you publish the project. Likewise, any settings that you create in your function app in the portal must be downloaded to your local project.

Publish application settings

The easiest way to publish the required settings to your function app in Azure is to use the Upload settings link that appears after you publish your project:

Upload application settings

You can also publish settings by using the Azure Functions: Upload Local Setting command in the command palette. You can add individual settings to application settings in Azure by using the Azure Functions: Add New Setting command.

Tip

Be sure to save your local.settings.json file before you publish it.

If the local file is encrypted, it's decrypted, published, and encrypted again. If there are settings that have conflicting values in the two locations, you're prompted to choose how to proceed.

View existing app settings in the Azure: Functions area by expanding your subscription, your function app, and Application Settings.

View function app settings in Visual Studio Code

Download settings from Azure

If you've created application settings in Azure, you can download them into your local.settings.json file by using the Azure Functions: Download Remote Settings command.

As with uploading, if the local file is encrypted, it's decrypted, updated, and encrypted again. If there are settings that have conflicting values in the two locations, you're prompted to choose how to proceed.

Monitoring functions

When you run functions locally, log data is streamed to the Terminal console. You can also get log data when your Functions project is running in a function app in Azure. You can either connect to streaming logs in Azure to see near-real-time log data, or you can enable Application Insights for a more complete understanding of how your function app is behaving.

Streaming logs

When you're developing an application, it's often useful to see logging information in near-real time. You can view a stream of log files being generated by your functions. This output is an example of streaming logs for a request to an HTTP-triggered function:

Streaming logs output for HTTP trigger

To learn more, see Streaming logs.

To turn on the streaming logs for your function app in Azure:

  1. Select F1 to open the command palette, and then search for and run the command Azure Functions: Start Streaming Logs.

  2. Select your function app in Azure, and then select Yes to enable application logging for the function app.

  3. Trigger your functions in Azure. Notice that log data is displayed in the Output window in Visual Studio Code.

  4. When you're done, remember to run the command Azure Functions: Stop Streaming Logs to disable logging for the function app.

Note

Streaming logs support only a single instance of the Functions host. When your function is scaled to multiple instances, data from other instances isn't shown in the log stream. Live Metrics Stream in Application Insights does support multiple instances. While also in near-real time, streaming analytics is based on sampled data.

Application Insights

We recommend that you monitor the execution of your functions by integrating your function app with Application Insights. When you create a function app in the Azure portal, this integration occurs by default. When you create your function app during Visual Studio publishing, you need to integrate Application Insights yourself.

Functions makes it easy to add Application Insights integration to a function app from the Azure portal.

  1. In the portal, select All services > Function Apps, select your function app, and then select the Application Insights banner at the top of the window

    Enable Application Insights from the portal

  2. Create an Application Insights resource by using the settings specified in the table below the image.

    Create an Application Insights resource

    Setting Suggested value Description
    Name Unique app name It's easiest to use the same name as your function app, which must be unique in your subscription.
    Location West Europe If possible, use the same region as your function app, or one that's close to that region.
  3. Select OK. The Application Insights resource is created in the same resource group and subscription as your function app. After the resource is created, close the Application Insights window.

  4. Back in your function app, select Application settings, and then scroll down to Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY, Application Insights integration is enabled for your function app running in Azure.

To learn more, see Monitor Azure Functions.

C# script projects

By default, all C# projects are created as C# compiled class library projects. If you prefer to work with C# script projects instead, you must select C# script as the default language in the Azure Functions extension settings:

  1. Select File > Preferences > Settings.

  2. Go to User Settings > Extensions > Azure Functions.

  3. Select C#Script from Azure Function: Project Language.

After you complete these steps, calls made to the underlying Core Tools include the --csx option, which generates and publishes C# script (.csx) project files. When you have this default language specified, all projects that you create default to C# script projects. You're not prompted to choose a project language when a default is set. To create projects in other languages, you must change this setting or remove it from the user settings.json file. After you remove this setting, you're again prompted to choose your language when you create a project.

Command palette reference

The Azure Functions extension provides a useful graphical interface in the area for interacting with your function apps in Azure. The same functionality is also available as commands in the command palette (F1). These Azure Functions commands are available:

Azure Functions command Description
Add New Settings Creates a new application setting in Azure. To learn more, see Publish application settings. You might also need to download this setting to your local settings.
Configure Deployment Source Connects your function app in Azure to a local Git repository. To learn more, see Continuous deployment for Azure Functions.
Connect to GitHub Repository Connects your function app to a GitHub repository.
Copy Function URL Gets the remote URL of an HTTP-triggered function that's running in Azure. To learn more, see Get the URL of the deployed function.
Create function app in Azure Creates a new function app in your subscription in Azure. To learn more, see the section on how to publish to a new function app in Azure.
Decrypt Settings Decrypts local settings that have been encrypted by Azure Functions: Encrypt Settings.
Delete Function App Removes a function app from your subscription in Azure. When there are no other apps in the App Service plan, you're given the option to delete that too. Other resources, like storage accounts and resource groups, aren't deleted. To remove all resources, you should instead delete the resource group. Your local project isn't affected.
Delete Function Removes an existing function from a function app in Azure. Because this deletion doesn't affect your local project, instead consider removing the function locally and then republishing your project.
Delete Proxy Removes an Azure Functions proxy from your function app in Azure. To learn more about proxies, see Work with Azure Functions Proxies.
Delete Setting Deletes a function app setting in Azure. This deletion doesn't affect settings in your local.settings.json file.
Disconnect from Repo Removes the continuous deployment connection between a function app in Azure and a source control repository.
Download Remote Settings Downloads settings from the chosen function app in Azure into your local.settings.json file. If the local file is encrypted, it's decrypted, updated, and encrypted again. If there are settings that have conflicting values in the two locations, you're prompted to choose how to proceed. Be sure to save changes to your local.settings.json file before you run this command.
Edit settings Changes the value of an existing function app setting in Azure. This command doesn't affect settings in your local.settings.json file.
Encrypt settings Encrypts individual items in the Values array in the local settings. In this file, IsEncrypted is also set to true, which specifies that the local runtime will decrypt settings before using them. Encrypt local settings to reduce the risk of leaking valuable information. In Azure, application settings are always stored encrypted.
Execute Function Now Manually starts a timer-triggered function in Azure. This command is used for testing. To learn more about triggering non-HTTP functions in Azure, see Manually run a non HTTP-triggered function.
Initialize Project for Use with VS Code Adds the required Visual Studio Code project files to an existing Functions project. Use this command to work with a project that you created by using Core Tools.
Install or Update Azure Functions Core Tools Installs or updates Azure Functions Core Tools, which is used to run functions locally.
Redeploy Lets you redeploy project files from a connected Git repository to a specific deployment in Azure. To republish local updates from Visual Studio Code, republish your project.
Rename Settings Changes the key name of an existing function app setting in Azure. This command doesn't affect settings in your local.settings.json file. After you rename settings in Azure, you should download those changes to the local project.
Restart Restarts the function app in Azure. Deploying updates also restarts the function app.
Set AzureWebJobsStorage Sets the value of the AzureWebJobsStorage application setting. This setting is required by Azure Functions. It's set when a function app is created in Azure.
Start Starts a stopped function app in Azure.
Start Streaming Logs Starts the streaming logs for the function app in Azure. Use streaming logs during remote troubleshooting in Azure if you need to see logging information in near-real time. To learn more, see Streaming logs.
Stop Stops a function app that's running in Azure.
Stop Streaming Logs Stops the streaming logs for the function app in Azure.
Toggle as Slot Setting When enabled, ensures that an application setting persists for a given deployment slot.
Uninstall Azure Functions Core Tools Removes Azure Functions Core Tools, which is required by the extension.
Upload Local Settings Uploads settings from your local.settings.json file to the chosen function app in Azure. If the local file is encrypted, it's decrypted, uploaded, and encrypted again. If there are settings that have conflicting values in the two locations, you're prompted to choose how to proceed. Be sure to save changes to your local.settings.json file before you run this command.
View Commit in GitHub Shows you the latest commit in a specific deployment when your function app is connected to a repository.
View Deployment Logs Shows you the logs for a specific deployment to the function app in Azure.

Next steps

To learn more about Azure Functions Core Tools, see Work with Azure Functions Core Tools.

To learn more about developing functions as .NET class libraries, see Azure Functions C# developer reference. This article also provides links to examples of how to use attributes to declare the various types of bindings supported by Azure Functions.