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. The warmup trigger lets you define a function that runs when a new instance of your function app is started. You can use a warmup trigger to preload custom dependencies so your functions are ready to start processing requests immediately. Some actions for a warmup trigger might include opening connections, loading dependencies, or running any other custom logic before your app begins receiving traffic.
The following considerations apply when using a warmup trigger:
warmup
(case-insensitive).You can create a C# function by using one of the following C# modes:
The following example shows a C# function that runs on each new instance when added to your app.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace SampleApp
{
public static class Warmup
{
[Function(nameof(Warmup))]
public static void Run([WarmupTrigger] object warmupContext, FunctionContext context)
{
var logger = context.GetLogger(nameof(Warmup));
logger.LogInformation("Function App instance is now warm!");
}
}
}
The following example shows a C# function that runs on each new instance when added to your app.
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 that runs when each new instance is added to your app.
@FunctionName("Warmup")
public void warmup( @WarmupTrigger Object warmupContext, ExecutionContext context) {
context.getLogger().info("Function App instance is warm.");
}
The following example shows a JavaScript function with a warmup trigger that runs on each new instance when added to your app:
const { app } = require('@azure/functions');
app.warmup('warmupTrigger', {
handler: (warmupContext, context) => {
context.log('Function App instance is warm.');
},
});
The following example shows a warmup trigger in a function.json file and a JavaScript function that runs on each new instance when added to your 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 (warmupContext, context) {
context.log('Function App instance is warm.');
};
The following example shows a TypeScript function with a warmup trigger that runs on each new instance when added to your app:
import { app, InvocationContext, WarmupContext } from '@azure/functions';
export async function warmupFunction(warmupContext: WarmupContext, context: InvocationContext): Promise<void> {
context.log('Function App instance is warm.');
}
app.warmup('warmup', {
handler: warmupFunction,
});
TypeScript samples aren't documented for model v3.
Here's the function.json file:
{
"bindings": [
{
"type": "warmupTrigger",
"direction": "in",
"name": "warmupContext"
}
]
}
PowerShell example code pending.
The following example shows a warmup trigger in a function.json file and a Python function that runs on each new instance when it'is added to your app.
Your function must be named warmup
(case-insensitive) and there can only be one warmup function per app.
import logging
import azure.functions as func
app = func.FunctionApp()
@app.warm_up_trigger('warmup')
def warmup(warmup) -> None:
logging.info('Function App instance is warm')
For more information, see Configuration.
Here's the function.json file:
{
"bindings": [
{
"type": "warmupTrigger",
"direction": "in",
"name": "warmupContext"
}
]
}
For more information, see Configuration.
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.')
Annotations
Warmup triggers don't require annotations. Just use a name of warmup
(case-insensitive) for the FunctionName
annotation.
warmupTrigger
(case-insensitive).options
object passed to the app.warmup()
method.The following table explains the binding configuration properties that you set in the function.json file.
function.json property Description type Required - must be set towarmupTrigger
. direction Required - must be set to in
. name Required - the variable name used in function code. A name
of warmupContext
is recommended for the binding parameter. Configuration
The following table explains the binding configuration properties that you set in the function.json file.
function.json property Description type Required - must be set towarmupTrigger
. direction Required - must be set to in
. name Required - the variable name used in function code. A name
of warmupContext
is recommended for the binding parameter.
See the Example section for complete examples.
UsageThe following considerations apply to using a warmup function in C#:
warmup
(case-insensitive) using the Function
attribute.Microsoft.Azure.Functions.Worker.Extensions.Warmup
packagewarmup
(case-insensitive) using the FunctionName
attribute.3.0.5
of the Microsoft.Azure.WebJobs.Extensions
package, or a later version.WarmupContext
instance to the function.Your function must be named warmup
(case-insensitive) using the FunctionName
annotation.
See the list of considerations at the top of the page for general usage advice.
The function type in function.json must be set to warmupTrigger
.
The function type in function.json must be set to warmupTrigger
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4