Run Azure Functions locally

This article explains how to develop and test your Azure Functions code locally, before you deploy it to Azure. Running functions locally allows for faster iteration and debugging without incurring any costs or requiring a deployed function app.

Prerequisites

Before you start, ensure you have the following installed:

Setting up your local development environment

You can use various tools to set up your local environment. The most common approaches are using Visual Studio Code or the command line.

Using Visual Studio Code

The Azure Functions extension for VS Code simplifies the process. You can create new projects, build functions, and debug them directly within the editor.

  1. Open Visual Studio Code.
  2. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
  3. Type Azure Functions: Create New Project... and select it.
  4. Choose a folder for your project and select your preferred language.
  5. Follow the prompts to create your first function.

Using the Command Line Interface (CLI)

You can use the Azure Functions Core Tools to create and manage your function projects from the terminal.

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command to create a new function app project:
    func init MyFunctionProj --worker-runtime dotnet

    Replace dotnet with your desired runtime (e.g., node, python, powershell).

  4. Navigate into the project directory:
    cd MyFunctionProj
  5. Add a new function to your project:
    func new --name MyHttpTrigger --template "HTTP trigger" --authlevel "anonymous"

    Adjust the name, template, and authorization level as needed.

Running your functions locally

Once your project is set up, you can start the local Functions host.

Using Visual Studio Code

  1. Open your function project folder in VS Code.
  2. Go to the Run and Debug view (Ctrl+Shift+D or Cmd+Shift+D).
  3. Select the Attach to Functions Runtime configuration from the dropdown and press the green play button.

Using the Command Line Interface (CLI)

From your project's root directory, run:

func start

The Functions host will start and display the local endpoints for your functions. You can access them using your browser or tools like cURL.

Note: When running locally, the Functions host emulates the Azure Functions runtime environment. Some features might behave slightly differently compared to the cloud environment.

Debugging locally

Debugging locally is crucial for identifying and fixing issues. Both VS Code and the CLI offer debugging capabilities.

Debugging with Visual Studio Code

After attaching the debugger (as described in "Running your functions locally"), you can set breakpoints in your code and step through execution.

Debugging with the CLI

The func start command can be used with debugger attachments for specific languages. For example, for Node.js:

node --inspect node_modules/@azure/functions/bin/cli.js start

Then, attach a Node.js debugger to the specified port.

Testing your functions

Once your functions are running locally, you can test them:

  • HTTP Triggers: Use your web browser or tools like Postman, Insomnia, or cURL to send requests to the local endpoint displayed by func start.
  • Other Triggers: For triggers like Timer, Queue, or Blob, you might need to set up local emulators (e.g., Azure Storage Emulator) or manually simulate events. The Azure Functions Core Tools often provide ways to simulate these events.
Tip: For a more comprehensive testing experience, consider using the Azure Functions emulator for local development.

Common Local Development Scenarios

  • Connecting to Azure Storage: Configure your local application settings to connect to your Azure Storage account or use the Azure Storage Emulator.
  • Local Settings: Use the local.settings.json file to store app settings, connection strings, and environment variables for your local development environment.
  • Function Project Structure: Understand the standard project structure generated by the Functions Core Tools for easy navigation and code management.

Limitations of Local Development

While powerful, local development has some limitations:

  • Managed Identities: Local development typically doesn't support managed identities directly. You'll often need to use service principals or connection strings for authentication.
  • Platform-Specific Triggers: Some triggers that rely on specific Azure services might require emulators or specific configurations to work locally.
  • Scale & Performance: Local environments don't fully replicate the scale and performance characteristics of the Azure Functions runtime.

Next Steps

After successfully running and debugging your functions locally, you're ready to deploy them to Azure. Refer to the deployment guides for your specific language and platform.