Use of Environment Variable inside Azure Function in C#
In this blog, we will learn how to configure and use Environment Variable in Azure Function. Concept of using Environment variable during your development which needs to deploy on multiple servers and connect to multiple systems. Whenever you use Environment variables to store the global constant or system credentials it reduces the time which was required to modify the code base for multiple deployments.
Let’s get started with Creating the Azure Function and using the Environment variable.
Step 1: Create an Azure Function inside the Visual Studio:
I have created the HTTP Trigger Azure function inside Visual Studio and the authorization level is Anonymous
Once you create an Azure Function project you will notice there are many files created with the project. We have a “local.setting.json” file that we are going to use and it holds the environment variable while the development and testing phase of your Azure function.
Step 2: Declare credentials/global variable inside “local.setting.json”
Information on local.setting.json – This file represents your local application setting for your Azure Function and it stays with your local build only. While you publish your azure function to Azure Portal or commit your code to your Repo this file is never pushed to the server as they are excluded/prevent from committing or publishing.
Step 3: Access variable inside the code
You can access the environment variable using the Environment class(required System lib). Based on your preference you can either access Environment variable the directly inside the code or you can create a CONSTANT class as mentioned below screenshot.
Here, I am passing this environment variable inside a response so that we can test it.
Step 4: Testing using Postman
We will require the API testing tool, here I am using Postman and the following is the link to download “Postman”. https://www.postman.com/downloads/
To test the application, click on the Start button on top of the Navbar as mentioned below in the screenshot [Button will have Project Name]. It will take a few minutes to Load the Azure Emulator
Following is the screen you will be able to see and copy the URL highlighted in the red below and paste that URL into Postman.
Now, you need to configure the Environment while deploying the Azure function on Azure Portal, and below are steps for the same:
Step 1: Deploy an Azure Function on Azure Portal.
You can directly create it on Portal or you can create it from Visual Studio. Here I am going to create and deploy it from Visual Studio:
Right Click on the project and select Publish:
Select Azure and Click on Next:
Click on the + icon to create a new function,
You can use the existing resource group or create a new resource group as per your preference:
After deployment is completed you can see the deployed Azure function on Azure Portal:
Now, if you try to use the Azure Function URL and post the request then you will notice that all the environmental credentials part is missing as we have not configured the environment variable on the Azure portal inside Azure Function App:
Step 2: Configure the App Environment variable on Azure Portal for online deployment.
Navigate to the Configuration section of your deployed Azure Function
Add the Application setting in your Configuration and the Name should same as you have declared in your local.setting.json file. Make sure your click on Save to update the configuration on Portal.
After configuration if you test your deployed Azure function using postman you will get your expected result:
This is how you can configure and use the Environment variable inside your Azure Function. It will make your life easier while deploying Azure on Development, UAT, or Production Environment. You can store the Source and Destination system credentials, SQL Access credentials, or Custom Global Variable based on your business requirement of integration.