MSDN Documentation

Azure Functions Local Development

This tutorial guides you through setting up and using Azure Functions for local development. Developing your functions locally allows for faster iteration, easier debugging, and a more streamlined development workflow before deploying to Azure.

Prerequisites

Before you begin, ensure you have the following installed:

Setting up your Development Environment

The Azure Functions Core Tools provide a command-line interface for creating, developing, testing, and debugging Azure Functions projects locally.

Creating a New Project

Open your terminal or command prompt and run the following command to create a new Functions project:

func init MyFunctionsProject --worker-runtime node

This command initializes a new project named MyFunctionsProject using the Node.js runtime. You can replace node with other supported runtimes like dotnet, python, or powershell.

Adding a New Function

Navigate into your project directory and add a new function using the func new command:

cd MyFunctionsProject
func new --name HttpTrigger --template "HTTP trigger" --authlevel "anonymous"

This creates an HTTP-triggered function named HttpTrigger that doesn't require authentication.

Running Functions Locally

To run your Azure Functions project locally, use the func start command in your project's root directory:

func start

This will build and start the Functions host. You will see output indicating that your functions are running and available at specific local URLs. For an HTTP trigger, it will typically look something like this:

...
For detailed output, run func with --verbose flag.
[2023-10-27T10:30:00.123Z] Host initialized.
[2023-10-27T10:30:01.456Z] Worker process started and initialized.
[2023-10-27T10:30:02.789Z] Functions are loaded.
[2023-10-27T10:30:03.012Z] Listening on 127.0.0.1:7071
[2023-10-27T10:30:03.013Z] HttpTrigger: [GET,POST] http://localhost:7071/api/HttpTrigger
...

You can then access your HTTP-triggered function by navigating to http://localhost:7071/api/HttpTrigger in your web browser or using tools like Postman.

Developer Note: When running locally, Functions emulates the Azure environment. This includes local storage for stateful functions (like Durable Functions) and local emulators for services like Azure Storage and Cosmos DB.

Debugging with Visual Studio Code

Visual Studio Code, along with the Azure Functions extension, provides an excellent debugging experience for local development.

  1. Open your Functions project folder in VS Code.
  2. Ensure you have the Azure Functions extension installed.
  3. In the Azure extension sidebar, under "Workspace (Local)", you should see your project.
  4. Click the "Start Debugging" button (usually a green play icon) or press F5.
  5. The extension will automatically configure and start the Functions host with the debugger attached.
  6. Set breakpoints in your function code by clicking in the gutter next to the line numbers.
  7. When your function is invoked, execution will pause at the breakpoint, allowing you to inspect variables, step through code, and diagnose issues.
Pro Tip: Configure launch.json in your project's .vscode folder for more advanced debugging scenarios, such as attaching to an already running process or specifying different launch configurations.

Working with Bindings and Triggers Locally

Azure Functions Core Tools supports emulating various Azure services locally, allowing you to test bindings and triggers without needing to connect to actual Azure resources.

local.settings.json

The local.settings.json file is crucial for configuring local development settings, including application settings, connection strings, and environment variables.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "MyCustomSetting": "This is a local setting"
  }
}

The AzureWebJobsStorage setting is essential for many bindings and features. Setting it to UseDevelopmentStorage=true often enables local storage emulators.

Important: The settings in local.settings.json are not deployed to Azure. You must configure equivalent settings in your Azure Function App's configuration.

Conclusion

Local development is a cornerstone of efficient Azure Functions development. By leveraging the Azure Functions Core Tools and Visual Studio Code, you can build, test, and debug your serverless applications with ease and confidence, leading to quicker development cycles and more robust solutions.