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

1

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

Ventriculus answered 6/9, 2017 at 22:21 Comment(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.Crotchet
Does this answer your question? Staging or Production Instance?Catchpenny
G
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.

Gonidium answered 7/9, 2017 at 2:44 Comment(4)
Wonderful, this is what I was looking for.Ventriculus
This env variable doesn't seem to exist any more :(Pout
The environment variable is "WEBSITE_SLOT_NAME" as of this date.Sin
I can see both APPSETTING_WEBSITE_SLOT_NAME and WEBSITE_SLOT_NAME variables in Kudu as of this dateBiotite

© 2022 - 2024 — McMap. All rights reserved.