If you are on the free Azure Function plan and have a function that gets called infrequently, you will likely start to see 502 errors when the function does get called, due to cold starts.
Most solutions online will tell you to turn on the ‘always on’ setting, but this is not available on the free plan.
The only way I have found (so far) to get around this is to add a new timer function that is frequently called and keeps the function app hot, thereby preventing cold start issues.
Note, this probably won’t work for functions that have very lumpy, but high call distributions. As this will only keep a single instance hot.
Here’s how to do it:
Add a new TimerTrigger function, set to be called every 3 minutes
public class KeepAlive
{
[Function("KeepAlive")]
public void Run([TimerTrigger("0 */3 * * * *")] FunctionContext context)
{
var logger = context.GetLogger("KeepAlive");
logger.LogInformation("KeepAlive");
}
}
While testing this I see the function app cycle to a new server every 3 to 4 calls to this function, so about every 9 to 12 minutes.
How to identify function call in Application Insights
If you want to do any analytics on calls to this function, you can use this:
traces
| where message == "KeepAlive"
| order by timestamp desc