Azure Functions Documentation

Develop Azure Functions Locally

Developing Azure Functions locally allows for rapid iteration and debugging before deploying to the cloud. This guide covers the essential tools and workflows for local development.

Prerequisites

Setting up Your Local Development Environment

The Azure Functions Core Tools provide a command-line interface (CLI) for creating, running, and debugging functions locally. You can also use the Azure Functions extension for Visual Studio Code for a more integrated experience.

Using Visual Studio Code

  1. Install the Azure Functions extension for VS Code.
  2. Open VS Code and create a new Azure Functions project:
    • Press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.
    • Type Azure Functions: Create New Project... and select it.
    • Choose a folder for your project.
    • Select your preferred language.
    • Choose a template for your first function (e.g., HTTP Trigger).
    • Provide a name for your function and a security authorization level.

Using the Azure Functions Core Tools CLI

You can create a new project from your terminal:

func init MyProject --worker-runtime 
cd MyProject
func new --template "HTTP trigger" --name MyHttpTrigger

Replace <runtime> with your chosen language runtime (e.g., dotnet, node, python).

Running and Debugging Locally

Once your project is set up, you can run your functions locally. This allows you to test their behavior and use your IDE's debugger.

Running with VS Code

  1. Press F5 or click the "Run and Debug" icon in the left sidebar.
  2. The Azure Functions host will start, and your functions will be available at their local endpoints.
  3. You can set breakpoints in your code and step through execution.

Running with the CLI

Navigate to your project directory in the terminal and run:

func start

This command starts the local Azure Functions host, which listens for requests to your functions.

Important: When running locally, your functions are accessible via localhost on a specific port (usually 7071).

Testing Your Functions

You can test your functions in various ways:

Example: Testing an HTTP Trigger

If you created an HTTP trigger named MyHttpTrigger, you can test it by navigating to a URL like:

http://localhost:7071/api/MyHttpTrigger

You can also send POST requests with a JSON body:

curl -X POST http://localhost:7071/api/MyHttpTrigger -H "Content-Type: application/json" -d '{"name": "World"}'

Local Configuration

Configuration settings, such as connection strings or application settings, are typically managed in a local.settings.json file within your project. This file is not deployed to Azure but is used by the local runtime.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}
For security reasons, avoid storing sensitive information directly in local.settings.json if your project is under source control. Use environment variables or Azure Key Vault integration for production secrets.

Best Practices for Local Development

By mastering local development, you can build and test your Azure Functions efficiently, leading to a smoother and more reliable deployment experience.