Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if an Azure function is running in a slot

Can a function know if it's running in a slot?

I would like to prevent a function to be executed if in "staging" slot.

Updated

Base on the answer from Bruce (thanks again buddy), I wrote that blog post http://www.frankysnotes.com/2017/09/how-to-know-when-azure-function-is.html

like image 982
Frank Boucher Avatar asked Sep 06 '17 22:09

Frank Boucher


People also ask

How do I know if Azure is running?

Navigate to your function app in the Azure portal. Select Diagnose and solve problems to open Azure Functions diagnostics.

What determines when an Azure function executes?

A trigger is responsible for executing an Azure function and there are dozens of triggers to choose from. This module will you show you some of the most common types of triggers and how to configure them to execute your logic.

Are Azure Functions always running?

On an App Service plan, the functions runtime goes idle after a few minutes of inactivity, so only HTTP triggers will "wake up" your functions. The Always on setting is available only on an App Service plan. On a Consumption plan, the platform activates function apps automatically.

What is slot setting in Azure?

Azure Functions deployment slots allow your function app to run different instances called "slots". Slots are different environments exposed via a publicly available endpoint. One app instance is always mapped to the production slot, and you can swap instances assigned to a slot on demand.


1 Answers

Can a function know if it's running in a slot?

Per my understanding, you could check the APPSETTING_WEBSITE_SLOT_NAME environment variable in your code, and the value would be Production when your function is under production while the value would be the slot name when your function is under a slot as follows:

enter image description here

enter image description here

For C#, you could leverage the following code for retrieving this variable:

System.Environment.GetEnvironmentVariable("APPSETTING_WEBSITE_SLOT_NAME", EnvironmentVariableTarget.Process);

Additionally, for F# you could refer to here, for JavaScript you could refer to here.

like image 53
Bruce Chen Avatar answered Sep 28 '22 10:09

Bruce Chen