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.

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 aren't 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 and search for and run the command Azure Functions: Create New Project.... Select the directory location for your project workspace, and 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 you can select Skip for now to create a project without a function. You can always add a function to your project later.

    Tip

    You can view additional templates by selecting the Change template filter option and setting the value to Core or All.

  4. For the function name, enter HttpExample, select Enter, and 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 using the func init --docker-only command in Core Tools.

Depending on your language, these other files are created:

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.

  • 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 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're able to run your HTTP trigger function locally.

Add a function to your project

You can add a new function to an existing project based on one of the predefined Functions trigger templates. To add a new function trigger, select F1 to open the command palette, and 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 depends 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 you 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 using the storageQueue function on the output object, which 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:

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

The following example shows the function definition after adding a Queue Storage output binding to an HTTP triggered function:

[FunctionName("HttpExample")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, 
    [Queue("outqueue"),StorageAccount("AzureWebJobsStorage")] ICollector<string> msg, 
    ILogger log)

The way you define the output binding depends on your process model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.

@FunctionName("HttpExample")
public HttpResponseMessage run(
        @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) 
        HttpRequestMessage<Optional<String>> request, 
        @QueueOutput(name = "msg", queueName = "outqueue", 
        connection = "AzureWebJobsStorage") OutputBinding<String> msg, 
        final ExecutionContext context) {

For more information, including links to example binding code that you can refer to, see Add bindings to a function.

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "queue",
      "direction": "out",
      "name": "msg",
      "queueName": "outqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

The way you define the output binding depends on the version of your Node.js model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.

$outputMsg = $name
Push-OutputBinding -name msg -Value $outputMsg

For more information, including links to example binding code that you can refer to, see Add bindings to a function.

@app.route(route="HttpExample")
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
def HttpExample(req: func.HttpRequest, msg: func.Out [func.QueueMessage]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

The way you define the output binding depends on the version of your Python model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "queue",
      "direction": "out",
      "name": "msg",
      "queueName": "outqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

The way you define the output binding depends on the version of your Node.js model. For more information, including links to example binding code that you can refer to, see Add bindings to a function.

Sign in to Azure

Before you can publish your app, you must sign in to Azure.

  1. If you aren't already signed in, choose the Azure icon in the Activity bar. Then in the Resources area, choose Sign in to Azure....

    Screenshot of the sign-in to Azure window within VS Code.

    If you're already signed in and can see your existing subscriptions, go to the next section. If you don't yet have an Azure account, choose Create an Azure Account.... Students can choose Create an Azure for Students Account....

  2. When prompted in the browser, choose your Azure account and sign in using your Azure account credentials. If you create a new account, you can sign in after your account is created.

  3. After you've successfully signed in, you can close the new browser window. The subscriptions that belong to your Azure account are displayed in the sidebar.

Create Azure resources

Before you can publish your Functions project to Azure, you must have a function app and related resources in your Azure subscription to run your code. The function app provides an execution context for your functions. When you publish from Visual Studio Code to a function app in Azure, the project is packaged and deployed to the selected function app in your Azure subscription.

When you create a function app in Azure, you can choose either a quick function app create path using defaults or a path that gives you advanced options, such as using existing Azure resources. This way, you have more control over creating the remote resources.

In this section, you create a function app and related resources in your Azure subscription.

  1. Choose the Azure icon in the Activity bar. Then in the Resources area, select the + icon and choose the Create Function App in Azure option.

    Create a resource in your Azure subscription

  2. Provide the following information at the prompts:

    Prompt Selection
    Select subscription Choose the subscription to use. You won't see this prompt when you have only one subscription visible under Resources.
    Enter a globally unique name for the function app Type a name that is valid in a URL path. The name you type is validated to make sure that it's unique in Azure Functions.
    Select a runtime stack Choose the language version on which you've been running locally.
    Select a location for new resources For better performance, choose a region near you.

    The extension shows the status of individual resources as they're being created in Azure in the Azure: Activity Log panel.

    Log of Azure resource creation

  3. When the creation is complete, the following Azure resources are created in your subscription. The resources are named based on your function app name:

    • A resource group, which is a logical container for related resources.
    • A standard Azure Storage account, which maintains state and other information about your projects.
    • A function app, which provides the environment for executing your function code. A function app lets you group functions as a logical unit for easier management, deployment, and sharing of resources within the same hosting plan.
    • An App Service plan, which defines the underlying host for your function app.
    • An Application Insights instance connected to the function app, which tracks usage of your functions in the app.

    A notification is displayed after your function app is created and the deployment package is applied.

    Tip

    By default, the Azure resources required by your function app are created based on the function app name you provide. By default, they're also created in the same new resource group with the function app. If you want to either customize the names of these resources or reuse existing resources, you need to publish the project with advanced create options instead.