7

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

2
  • I'm very late to this, but if you're looking to disable a function you can use app settings, see learn.microsoft.com/en-us/azure/azure-functions/…. To disable a function in a particular slot, you can make this app setting a slot setting. There's no need to write any code, or check any environment variables, in your function to achieve this.
    – Chris
    Jan 24, 2021 at 19:51
  • Does this answer your question? Staging or Production Instance?
    – yoel halb
    May 11, 2022 at 20:40

1 Answer 1

12

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.

4
  • Wonderful, this is what I was looking for. Sep 7, 2017 at 9:04
  • 1
    This env variable doesn't seem to exist any more :( Jun 13, 2019 at 10:30
  • 1
    The environment variable is "WEBSITE_SLOT_NAME" as of this date. Dec 16, 2019 at 23:06
  • I can see both APPSETTING_WEBSITE_SLOT_NAME and WEBSITE_SLOT_NAME variables in Kudu as of this date Dec 8, 2020 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.