Uredi

Deli z drugimi prek


Develop Azure Functions locally using Core Tools

Azure Functions Core Tools lets you develop and test your functions on your local computer. When you're ready, you can also use Core Tools to deploy your code project to Azure and work with application settings.

You're viewing the C# version of this article. Make sure to select your preferred Functions programming language at the top of the article.

If you want to get started right away, complete the Core Tools quickstart article.

You're viewing the Java version of this article. Make sure to select your preferred Functions programming language at the top of the article.

If you want to get started right away, complete the Core Tools quickstart article.

You're viewing the JavaScript version of this article. Make sure to select your preferred Functions programming language at the top of the article.

If you want to get started right away, complete the Core Tools quickstart article.

You're viewing the PowerShell version of this article. Make sure to select your preferred Functions programming language at the top of the article.

If you want to get started right away, complete the Core Tools quickstart article.

You're viewing the Python version of this article. Make sure to select your preferred Functions programming language at the top of the article.

If you want to get started right away, complete the Core Tools quickstart article.

You're viewing the TypeScript version of this article. Make sure to select your preferred Functions programming language at the top of the article.

If you want to get started right away, complete the Core Tools quickstart article.

Install the Azure Functions Core Tools

The recommended way to install Core Tools depends on the operating system of your local development computer.

The following steps use a Windows installer (MSI) to install Core Tools v4.x. For more information about other package-based installers, see the Core Tools readme.

Download and run the Core Tools installer, based on your version of Windows:

If you previously used Windows installer (MSI) to install Core Tools on Windows, you should uninstall the old version from Add Remove Programs before installing the latest version.

For help with version-related issues, see Core Tools versions.

Create your local project

Important

For Python, you must run Core Tools commands in a virtual environment. For more information, see Quickstart: Create a Python function in Azure from the command line.

In the terminal window or from a command prompt, run the following command to create a project in the MyProjFolder folder:

func init MyProjFolder --worker-runtime dotnet-isolated 

By default this command creates a project that runs in-process with the Functions host on the current Long-Term Support (LTS) version of .NET Core. You can use the --target-framework option to target a specific supported version of .NET, including .NET Framework. For more information, see the func init reference.

For a comparison between the two .NET process models, see the process mode comparison article.

Java uses a Maven archetype to create the local project, along with your first HTTP triggered function. Rather than using func init and func new, you should instead follow the steps in the Command line quickstart.

func init MyProjFolder --worker-runtime javascript --model V4

This command creates a JavaScript project that uses the desired programming model version.

func init MyProjFolder --worker-runtime typescript --model V4

This command creates a TypeScript project that uses the desired programming model version.

func init MyProjFolder --worker-runtime powershell
func init MyProjFolder --worker-runtime python --model V2

This command creates a Python project that uses the desired programming model version.

When you run func init without the --worker-runtime option, you're prompted to choose your project language. To learn more about the available options for the func init command, see the func init reference.

Create a function

To add a function to your project, run the func new command using the --template option to select your trigger template. The following example creates an HTTP trigger named MyHttpTrigger:

func new --template "Http Trigger" --name MyHttpTrigger

This example creates a Queue Storage trigger named MyQueueTrigger:

func new --template "Azure Queue Storage Trigger" --name MyQueueTrigger

The following considerations apply when adding functions:

  • When you run func new without the --template option, you're prompted to choose a template.

  • Use the func templates list command to see the complete list of available templates for your language.

  • When you add a trigger that connects to a service, you'll also need to add an application setting that references a connection string or a managed identity to the local.settings.json file. Using app settings in this way prevents you from having to embed credentials in your code. For more information, see Work with app settings locally.

  • Core Tools also adds a reference to the specific binding extension to your C# project.

To learn more about the available options for the func new command, see the func new reference.

Add a binding to your function

Functions provides a set of service-specific input and output bindings, which make it easier for your function to connection to other Azure services without having to use the service-specific client SDKs. For more information, see Azure Functions triggers and bindings concepts.

To add an input or output binding to an existing function, you must manually update the function definition.

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

Because an HTTP triggered function also returns an HTTP response, the function returns a MultiResponse object, which represents both the HTTP and queue output.

[Function("HttpExample")]
public static MultiResponse Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
    FunctionContext executionContext)
{

This example is the definition of the MultiResponse object that includes the output binding:

public class MultiResponse
{
    [QueueOutput("outqueue",Connection = "AzureWebJobsStorage")]
    public string[] Messages { get; set; }
    public IActionResult HttpResponse { get; set; }
}

When applying that example to your own project, you might need to change HttpRequest to HttpRequestData and IActionResult to HttpResponseData, depending on if you are using ASP.NET Core integration or not.

Messages are sent to the queue when the function completes. 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.

Example binding for Node.js model v4 not yet available.

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.

Example binding for Node.js model v4 not yet available.

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.

The following considerations apply when adding bindings to a function:

  • For languages that define functions using the function.json configuration file, Visual Studio Code simplifies the process of adding bindings to an existing function definition. For more information, see Connect functions to Azure services using bindings.
  • When you add bindings that connect to a service, you must also add an application setting that references a connection string or managed identity to the local.settings.json file. For more information, see Work with app settings locally.
  • When you add a supported binding, the extension should already be installed when your app uses extension bundle. For more information, see extension bundles.
  • When you add a binding that requires a new binding extension, you must also add a reference to that specific binding extension in your C# project.

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

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

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

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

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

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

Start the Functions runtime

Before you can run or debug the functions in your project, you need to start the Functions host from the root directory of your project. The host enables triggers for all functions in the project. Use this command to start the local runtime:

mvn clean package 
mvn azure-functions:run
func start
func start
npm install
npm start     

This command must be run in a virtual environment.

When the Functions host starts, it outputs a list of functions in the project, including the URLs of any HTTP-triggered functions, like in this example:

Found the following functions:
Host.Functions.MyHttpTrigger

Job host started
Http Function MyHttpTrigger: http://localhost:7071/api/MyHttpTrigger

Keep in mind the following considerations when running your functions locally:

  • By default, authorization isn't enforced locally for HTTP endpoints. This means that all local HTTP requests are handled as authLevel = "anonymous". For more information, see Authorization level. You can use the --enableAuth option to require authorization when running locally. For more information, see func start

  • You can use the local Azurite emulator when locally running functions that require access to Azure Storage services (Queue Storage, Blob Storage, and Table Storage) without having to connect to these services in Azure. When using local emulation, make sure to start Azurite before starting the local host (func.exe). For more information, see Local storage emulation.

  • You can use local Azurite emulation to meet the storage requirement of the Python v2 worker.
  • You can trigger non-HTTP functions locally without connecting to a live service. For more information, see Run a local function.

  • When you include your Application Insights connection information in the local.settings.json file, local log data is written to the specific Application Insights instance. To keep local telemetry data separate from production data, consider using a separate Application Insights instance for development and testing.

  • When using version 1.x of the Core Tools, instead use the func host start command to start the local runtime.

Run a local function

With your local Functions host (func.exe) running, you can now trigger individual functions to run and debug your function code. The way in which you execute an individual function depends on its trigger type.

Note

Examples in this topic use the cURL tool to send HTTP requests from the terminal or a command prompt. You can use a tool of your choice to send HTTP requests to the local server. The cURL tool is available by default on Linux-based systems and Windows 10 build 17063 and later. On older Windows, you must first download and install the cURL tool.

HTTP triggers are started by sending an HTTP request to the local endpoint and port as displayed in the func.exe output, which has this general format:

http://localhost:<PORT>/api/<FUNCTION_NAME>

In this URL template, <FUNCTION_NAME> is the name of the function or route and <PORT> is the local port on which func.exe is listening.

For example, this cURL command triggers the MyHttpTrigger quickstart function from a GET request with the name parameter passed in the query string:

curl --get http://localhost:7071/api/MyHttpTrigger?name=Azure%20Rocks

This example is the same function called from a POST request passing name in the request body, shown for both Bash shell and Windows command line:

curl --request POST http://localhost:7071/api/MyHttpTrigger --data '{"name":"Azure Rocks"}'
curl --request POST http://localhost:7071/api/MyHttpTrigger --data "{'name':'Azure Rocks'}"

The following considerations apply when calling HTTP endpoints locally:

  • You can make GET requests from a browser passing data in the query string. For all other HTTP methods, you must use a HTTP testing tool that also keeps your data secure. For more information, see HTTP test tools.

  • Make sure to use the same server name and port that the Functions host is listening on. You see an endpoint like this in the output generated when starting the Function host. You can call this URL using any HTTP method supported by the trigger.

Publish to Azure

The Azure Functions Core Tools supports three types of deployment:

Deployment type Command Description
Project files func azure functionapp publish Deploys function project files directly to your function app using zip deployment.
Azure Container Apps func azurecontainerapps deploy Deploys a containerized function app to an existing Container Apps environment.
Kubernetes cluster func kubernetes deploy Deploys your Linux function app as a custom Docker container to a Kubernetes cluster.

You must have either the Azure CLI or Azure PowerShell installed locally to be able to publish to Azure from Core Tools. By default, Core Tools uses these tools to authenticate with your Azure account.

If you don't have these tools installed, you need to instead get a valid access token to use during deployment. You can present an access token using the --access-token option in the deployment commands.

Deploy project files

To publish your local code to a function app in Azure, use the func azure functionapp publish command, as in the following example:

func azure functionapp publish <FunctionAppName>

This command publishes project files from the current directory to the <FunctionAppName> as a .zip deployment package. If the project requires compilation, it's done remotely during deployment.

Java uses Maven to publish your local project to Azure instead of Core Tools. Use the following Maven command to publish your project to Azure:

mvn azure-functions:deploy

When you run this command, Azure resources are created during the initial deployment based on the settings in your pom.xml file. For more information, see Deploy the function project to Azure.

The following considerations apply to this kind of deployment:

Deploy containers

Core Tools lets you deploy your containerized function app to both managed Azure Container Apps environments and Kubernetes clusters that you manage.

Use the following func azurecontainerapps deploy command to deploy an existing container image to a Container Apps environment:

func azurecontainerapps deploy --name <APP_NAME> --environment <ENVIRONMENT_NAME> --storage-account <STORAGE_CONNECTION> --resource-group <RESOURCE_GROUP> --image-name <IMAGE_NAME> [--registry-password] [--registry-server] [--registry-username]

When you deploy to an Azure Container Apps environment, the following considerations apply:

  • The environment and storage account must already exist. The storage account connection string you provide is used by the deployed function app.

  • You don't need to create a separate function app resource when deploying to Container Apps.

  • Storage connection strings and other service credentials are important secrets. Make sure to securely store any script files using func azurecontainerapps deploy and don't store them in any publicly accessible source control systems. You can encrypt the local.settings.json file for added security.

For more information, see Azure Container Apps hosting of Azure Functions.

Work with app settings locally

When running in a function app in Azure, settings required by your functions are stored securely in app settings. During local development, these settings are instead added to the Values collection in the local.settings.json file. The local.settings.json file also stores settings used by local development tools.

Items in the Values collection in your project's local.settings.json file are intended to mirror items in your function app's application settings in Azure.

The following considerations apply when working with the local settings file:

  • Because the local.settings.json may contain secrets, such as connection strings, you should never store it in a remote repository. Core Tools helps you encrypt this local settings file for improved security. For more information, see Local settings file. You can also encrypt the local.settings.json file for added security.

  • By default, local settings aren't migrated automatically when the project is published to Azure. Use the --publish-local-settings option when you publish your project files to make sure these settings are added to the function app in Azure. Values in the ConnectionStrings section are never published. You can also upload settings from the local.settings.json file at any time.

  • You can download and overwrite settings in your local.settings.json file with settings from your function app in Azure. For more information, see Download application settings.

  • The function app settings values can also be read in your code as environment variables. For more information, see Environment variables.
  • The function app settings values can also be read in your code as environment variables. For more information, see Environment variables.
  • The function app settings values can also be read in your code as environment variables. For more information, see Environment variables.
  • The function app settings values can also be read in your code as environment variables. For more information, see Environment variables.
  • The function app settings values can also be read in your code as environment variables. For more information, see Environment variables.

Download application settings

From the project root, use the following command to download all application settings from the myfunctionapp12345 app in Azure:

func azure functionapp fetch-app-settings myfunctionapp12345

This command overwrites any existing settings in the local.settings.json file with values from Azure. When not already present, new items are added to the collection. For more information, see the func azure functionapp fetch-app-settings command.

Download a storage connection string

Core Tools also make it easy to get the connection string of any storage account to which you have access. From the project root, use the following command to download the connection string from a storage account named mystorage12345.

func azure storage fetch-connection-string mystorage12345

This command adds a setting named mystorage12345_STORAGE to the local.settings.json file, which contains the connection string for the mystorage12345 account. For more information, see the func azure storage fetch-connection-string command.

For improved security during development, consider encrypting the local.settings.json file.

Upload local settings to Azure

When you publish your project files to Azure without using the --publish-local-settings option, settings in the local.settings.json file aren't set in your function app. You can always rerun the func azure functionapp publish with the --publish-settings-only option to upload just the settings without republishing the project files.

The following example uploads just settings from the Values collection in the local.settings.json file to the function app in Azure named myfunctionapp12345:

func azure functionapp publish myfunctionapp12345 --publish-settings-only

Encrypt the local settings file

To improve security of connection strings and other valuable data in your local settings, Core Tools lets you encrypt the local.settings.json file. When this file is encrypted, the runtime automatically decrypts the settings when needed the same way it does with application setting in Azure. You can also decrypt a locally encrypted file to work with the settings.

Use the following command to encrypt the local settings file for the project:

func settings encrypt

Use the following command to decrypt an encrypted local setting, so that you can work with it:

func settings decrypt

When the settings file is encrypted and decrypted, the file's IsEncrypted setting also gets updated.

Configure binding extensions

Functions triggers and bindings are implemented as .NET extension (NuGet) packages. To be able to use a specific binding extension, that extension must be installed in the project.

This section doesn't apply to version 1.x of the Functions runtime. In version 1.x, supported bindings were included in the core product extension.

For C# class library projects, add references to the specific NuGet packages for the binding extensions required by your functions. C# script (.csx) project must use extension bundles.

Functions provides extension bundles to make is easy to work with binding extensions in your project. Extension bundles, which are versioned and defined in the host.json file, install a complete set of compatible binding extension packages for your app. Your host.json should already have extension bundles enabled. If for some reason you need to add or update the extension bundle in the host.json file, see Extension bundles.

If you must use a binding extension or an extension version not in a supported bundle, you need to manually install extensions. For such rare scenarios, see the func extensions install command.

Core Tools versions

Major versions of Azure Functions Core Tools are linked to specific major versions of the Azure Functions runtime. For example, version 4.x of Core Tools supports version 4.x of the Functions runtime. This version is the recommended major version of both the Functions runtime and Core Tools. You can determine the latest release version of Core Tools in the Azure Functions Core Tools repository.

Starting with version 4.0.6517 of the Core Tools, in-process model projects must reference version 4.5.0 or later of Microsoft.NET.Sdk.Functions. If an earlier version is used, the func start command will error.

Run the following command to determine the version of your current Core Tools installation:

func --version

Unless otherwise noted, the examples in this article are for version 4.x.

The following considerations apply to Core Tools installations:

  • You can only install one version of Core Tools on a given computer.

  • When upgrading to the latest version of Core Tools, you should use the same method that you used for original installation to perform the upgrade. For example, if you used an MSI on Windows, uninstall the current MSI and install the latest one. Or if you used npm, rerun the npm install command.

  • Version 2.x and 3.x of Core Tools were used with versions 2.x and 3.x of the Functions runtime, which have reached their end of support. For more information, see Azure Functions runtime versions overview.

  • Version 1.x of Core Tools is required when using version 1.x of the Functions Runtime, which is still supported. This version of Core Tools can only be run locally on Windows computers. If you're currently running on version 1.x, you should consider migrating your app to version 4.x today.

Next steps

Learn how to develop, test, and publish Azure functions by using Azure Functions core tools. Azure Functions Core Tools is open source and hosted on GitHub. To file a bug or feature request, open a GitHub issue.