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
Navigate to your function app in the Azure portal. Select Diagnose and solve problems to open Azure Functions diagnostics.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With