title | description | documentationcenter | author | manager | keywords | ms.service | ms.topic | ms.custom | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|
Azure Functions warmup trigger |
Understand how to use the warmup trigger in Azure Functions. |
na |
craigshoemaker |
gwallace |
azure functions, functions, event processing, warmup, cold start, premium, dynamic compute, serverless architecture |
azure-functions |
reference |
devx-track-csharp |
11/08/2019 |
cshoe |
This article explains how to work with the warmup trigger in Azure Functions. A warmup trigger is invoked when an instance is added to scale a running function app. You can use a warmup trigger to pre-load custom dependencies during the pre-warming process so that your functions are ready to start processing requests immediately.
Note
The warmup trigger isn't supported for function apps running in a Consumption plan.
[!INCLUDE intro]
The Microsoft.Azure.WebJobs.Extensions NuGet package, version 3.0.5 or higher is required. Source code for the package is in the azure-webjobs-sdk-extensions GitHub repository.
[!INCLUDE functions-package]
The warmup trigger lets you define a function that will be run on a new instance when it is added to your running app. You can use a warmup function to open connections, load dependencies, or run any other custom logic before your app will begin receiving traffic.
The warmup trigger is intended to create shared dependencies that will be used by the other functions in your app. See examples of shared dependencies here.
Note that the warmup trigger is only called during scale-out operations, not during restarts or other non-scale startups. You must ensure your logic can load all necessary dependencies without using the warmup trigger. Lazy loading is a good pattern to achieve this.
The following example shows a C# function that will run on each new instance when it is added to your app. A return value attribute isn't required.
- Your function must be named
warmup
(case-insensitive) and there may only be one warmup function per app. - To use warmup as a .NET class library function, please make sure you have a package reference to Microsoft.Azure.WebJobs.Extensions >= 3.0.5
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.5" />
Placeholder comments show where in the application to declare and initialize shared dependencies. Learn more about shared dependencies here.
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace WarmupSample
{
//Declare shared dependencies here
public static class Warmup
{
[FunctionName("Warmup")]
public static void Run([WarmupTrigger()] WarmupContext context,
ILogger log)
{
//Initialize shared dependencies here
log.LogInformation("Function App instance is warm 🌞🌞🌞");
}
}
}
The following example shows a warmup trigger in a function.json file and a C# script function that will run on each new instance when it is added to your app.
Your function must be named warmup
(case-insensitive), and there may only be one warmup function per app.
Here's the function.json file:
{
"bindings": [
{
"type": "warmupTrigger",
"direction": "in",
"name": "warmupContext"
}
]
}
The configuration section explains these properties.
public static void Run(WarmupContext warmupContext, ILogger log)
{
log.LogInformation("Function App instance is warm 🌞🌞🌞");
}
The following example shows a warmup trigger in a function.json file and a JavaScript function that will run on each new instance when it is added to your app.
Your function must be named warmup
(case-insensitive) and there may only be one warmup function per app.
Here's the function.json file:
{
"bindings": [
{
"type": "warmupTrigger",
"direction": "in",
"name": "warmupContext"
}
]
}
The configuration section explains these properties.
Here's the JavaScript code:
module.exports = async function (context, warmupContext) {
context.log('Function App instance is warm 🌞🌞🌞');
};
The following example shows a warmup trigger in a function.json file and a Python function that will run on each new instance when it is added to your app.
Your function must be named warmup
(case-insensitive) and there may only be one warmup function per app.
Here's the function.json file:
{
"bindings": [
{
"type": "warmupTrigger",
"direction": "in",
"name": "warmupContext"
}
]
}
The configuration section explains these properties.
Here's the Python code:
import logging
import azure.functions as func
def main(warmupContext: func.Context) -> None:
logging.info('Function App instance is warm 🌞🌞🌞')
The following example shows a warmup trigger that runs when each new instance is added to your app.
Your function must be named warmup
(case-insensitive) and there may only be one warmup function per app.
@FunctionName("Warmup")
public void run( ExecutionContext context) {
context.getLogger().info("Function App instance is warm 🌞🌞🌞");
}
In C# class libraries, the WarmupTrigger
attribute is available to configure the function.
This example demonstrates how to use the warmup attribute.
Note that your function must be called Warmup
and there can only be one warmup function per app.
[FunctionName("Warmup")]
public static void Run(
[WarmupTrigger()] WarmupContext context, ILogger log)
{
...
}
For a complete example, see the trigger example.
Attributes are not supported by C# Script.
Attributes are not supported by JavaScript.
Attributes are not supported by Python.
The warmup trigger is not supported in Java as an attribute.
The following table explains the binding configuration properties that you set in the function.json file and the WarmupTrigger
attribute.
function.json property | Attribute property | Description |
---|---|---|
type | n/a | Required - must be set to warmupTrigger . |
direction | n/a | Required - must be set to in . |
name | n/a | Required - the variable name used in function code. |
No additional information is provided to a warmup triggered function when it is invoked.
- The warmup trigger is only available to apps running on the Premium plan.
- The warmup trigger is only called during scale-out operations, not during restarts or other non-scale startups. You must ensure your logic can load all necessary dependencies without using the warmup trigger. Lazy loading is a good pattern to achieve this.
- The warmup trigger cannot be invoked once an instance is already running.
- There can only be one warmup trigger function per function app.