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:
- Azure Functions Core Tools
- Visual Studio Code (Recommended)
- Azure Functions extension for Visual Studio Code
- A supported programming language runtime (e.g., Node.js, Python, .NET)
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.
- Open Visual Studio Code.
- Open the Command Palette (
Ctrl+Shift+P
orCmd+Shift+P
). - Type
Azure Functions: Create New Project...
and select it. - Choose a folder for your project and select your preferred language.
- 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.
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your project.
- 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
). - Navigate into the project directory:
cd MyFunctionProj
- 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
- Open your function project folder in VS Code.
- Go to the Run and Debug view (
Ctrl+Shift+D
orCmd+Shift+D
). - 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.
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.
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.