Edit

Develop Azure Functions by using Visual Studio Code

The Azure Functions extension for Visual Studio Code lets you develop functions locally 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.

You're viewing the C# version of this article. Make sure to select your preferred Functions programming language at the start of the article.

If you're new to Functions, you might want to first complete the Visual Studio Code quickstart article.

You're viewing the Java version of this article. Make sure to select your preferred Functions programming language at the start of the article.

If you're new to Functions, you might want to first complete the Visual Studio Code quickstart article.

You're viewing the JavaScript version of this article. Make sure to select your preferred Functions programming language at the start of the article.

If you're new to Functions, you might want to first complete the Visual Studio Code quickstart article.

You're viewing the PowerShell version of this article. Make sure to select your preferred Functions programming language at the start of the article.

If you're new to Functions, you might want to first complete the Visual Studio Code quickstart article.

You're viewing the Python version of this article. Make sure to select your preferred Functions programming language at the start of the article.

If you're new to Functions, you might want to first complete the Visual Studio Code quickstart article.

You're viewing the TypeScript version of this article. Make sure to select your preferred Functions programming language at the start of the article.

If you're new to Functions, you might want to first complete the Visual Studio Code quickstart article.

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

You also need these prerequisites to run and debug your functions locally. They're not required to just create or publish projects to Azure Functions.

  • The Azure Functions Core Tools, which enables an integrated local debugging experience. When you have the Azure Functions extension installed, the easiest way to install or update Core Tools is by running the Azure Functions: Install or Update Azure Functions Core Tools command from the command palette.

Create an Azure Functions project

The Functions extension lets you create the required function app project at the same time you create your first function. Use these steps to create an HTTP-triggered function in a new project. An HTTP trigger is the simplest function trigger template to demonstrate.

  1. In Visual Studio Code, press F1 to open the command palette. Search for and run the command Azure Functions: Create New Project.... Select the directory location for your project workspace, then choose Select.

    You can either create a new folder or choose an empty folder for the project workspace, but don't choose a project folder that's already part of a workspace.

    You can instead run the command Azure Functions: Create New Containerized Project... to also get a Dockerfile generated for the project.

  2. When prompted, Select a language for your project. If necessary, choose a specific language version.

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

    Tip

    To view additional templates, select the Change template filter option and set the value to Core or All.

  4. For the function name, enter HttpExample, select Enter, then select Function authorization.

    This authorization level requires that you provide a function key when you call the function endpoint.

  5. From the dropdown list, select Add to workspace.

  6. In the Do you trust the authors of the files in this folder? window, select Yes.

Visual Studio Code creates a function in your chosen language and in the template for an HTTP-triggered function.

Generated project files

The project template creates a project in your chosen language and installs the 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 locally running functions. 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, make sure to exclude the file from your project source control.

  • Dockerfile (optional): Lets you create a containerized function app from your project by using an approved base image for your project. You only get this file when you run the command Azure Functions: Create New Containerized Project.... You can add a Dockerfile to an existing project by using the func init --docker-only command in Core Tools.

An HttpExample.cs class library file, the contents of which vary depending on whether your project runs in an isolated worker process or in-process with the Functions host.

These files are created:

  • A pom.xml file in the root folder that defines the project and deployment parameters, including project dependencies and the Java version. The pom.xml also contains information about the Azure resources that are created during a deployment.

  • A Functions.java file in your src path that implements the function.

Files generated depend on the chosen Node.js programming model for Functions:

  • A package.json file in the root folder.

  • A named .js file in the src\functions folder, which contains both the function definition and your function code.

An HttpExample folder is created that contains:

Files generated depend on the chosen Python programming model for Functions:

  • A project-level requirements.txt file that lists packages required by Functions.

  • A function_app.py file that contains both the function definition and code.

At this point, you can run your HTTP trigger function locally.

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, then find 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 that item ready before you create the function trigger.

This action adds a new C# class library (.cs) file to your project.

This action adds a new Java (.java) file to your project.

This action's results depend on the Node.js model version.

  • A package.json file in the root folder.

  • A named .js file in the src\functions folder, which contains both the function definition and your function code.

This action creates a new folder in the project. The folder contains a new function.json file and the new PowerShell code file.

This action's results depend on the Python model version.

Visual Studio Code adds new function code either to the function_app.py file (default behavior) or to another Python file that you selected.

Connect to services

You can connect your function to other Azure services by adding input and output bindings. Bindings connect your function to other services without you having to write the connection code.

For example, the way that you define an output binding that writes data to a storage queue depends on your process model:

  1. If necessary, add a reference to the package that supports your binding extension.

  2. Update the function method to add an attribute that defines the binding parameter, like QueueOutput for a queue output binding. You can use a MultiResponse object to return multiple messages or multiple output streams.

For example, to add an output binding that writes data to a storage queue, update the function method to add a binding parameter defined by using the QueueOutput annotation. The OutputBinding<T> object represents the messages that are written to an output binding when the function completes.

For example, the way that you define the output binding that writes data to a storage queue depends on your Node.js model version:

Using the Node.js v4 model, you must manually add a return: option in the function definition by using the storageQueue function on the output object. This function defines the storage queue to write the return output. The output is written when the function completes.

Visual Studio Code lets you add bindings to your function.json file by following a convenient set of prompts.

To add a binding, open the command pallet (F1) and type Azure Functions: add binding..., choose the function for the new binding, and then follow the prompts, which vary depending on the type of binding being added to the function.

The 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.settings.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.

You can also right-click (Ctrl+click on macOS) directly on the function.json file in your function folder, select Add binding, and follow the same prompts.

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"
}

For example, the way you define the output binding that writes data to a storage queue depends on your Python model version:

Use the @queue_output decorator on the function to define a named binding parameter for the output to the storage queue. The func.Out parameter defines what output is written.