In Azure Functions, Environment Variables are set for the Function App, and can be accessed by all the functions within that app. They are set within the ‘Configuration’ tab within Settings on the left pane. They are referred to as ‘Application Settings’.
As you can see in the above image, some application settings are present by default. To add your own application setting, click on ‘New application setting’, and provide it a name and a value.
You will see a checkbox reading ‘Deployment slot setting’. What does that mean? For that, we first need to understand what a ‘deployment slot’ means.
Azure Function Apps can have multiple instances running at the same time. These instances are called ‘slots’. Think of these instances like different environments: development, staging, production, etc. One instance is always the production slot, and you can swap the instances on demand. You can read more about deployment slots here. One important thing to note is that Azure Functions in the Consumption plan allow only one deployment slot.
Now, when you check the ‘Deployment slot setting’ checkbox, it means that you are ‘sticking’ that setting to a particular deployment slot, or a particular instance. If you don’t check that box, the application setting will be applied across the slots. Note again that this entire discussion doesn’t matter for apps in the Consumption plan, since they allow only one deployment slot.
Once you have set the name and value for the variable, and chosen the deployment slot setting, click OK. Then click on ‘Save’.
It will give you a disclaimer that your application will be restarted. Click on ‘Continue’. Your web app settings will be updated.
Within your function code, you can access this variable just like you access environment variables generally. An example with python is given below:
import os
new_db_flag = int(os.environ["NEW_DB_FLAG"])
If you wish to edit the environment variable, go to application settings, and click on the edit button against the variable you wish to change.
That’s it. I hoped this article helped.
If you liked this article, you may check out other articles related to Azure here: https://iotespresso.com/category/azure/
If you are planning to appear for the Azure Administrator Exam Certification, check out this course on Udemy.