Azure Function Environment Variable

Azure Function Environment Variable

In this Azure article, we will discuss Azure Function Environment Variable. How to access environment variables to use in your Azure Function, etc.

Azure Function Environment Variable

Environment Variable helps you to store any custom settings for your Azure Function. This is the main purpose of the Environment Variable.

You can configure or manage these custom settings in the Application Settings of the Azure Function App. To reach out to this option, You can log in to the Azure Portal and then navigate to your Azure Function App and then click on the Configuration from the left side menu on your Azure Function App page. Now you can find the option Application settings.

Azure Function Environment Variable

We can use the GetEnvironmentVariable() to access the value of the Environment Variable.

Example

If we will see an example, below is the MyNewHTTPNewFunction Azure Function. Where we have used the GetEnvironmentVariable method.

[FunctionName("MyNewHTTPNewFunction
")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("MyNewHTTPNewFunction processed a request.");
            log.LogInformation(GetEnvironmentVariable("MY_PRODUCT_NAME"));

var NewSetting =  Environment.GetEnvironmentVariable("MyNewSetting", EnvironmentVariableTarget.Process);
    return Task.FromResult(req.CreateResponse(HttpStatusCode.OK, new { setting=  NewSetting }));

Now in your local.settings.json file of your Azure Function, you should mention “MY_PRODUCT_NAME” and the value for this key. GetEnvironmentVariable(“MY_PRODUCT_NAME”) retrieves the value for this key which is “Your Product name” in this case.

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "MY_PRODUCT_NAME": "Your Product Name"

  }
}

You may also like following the below Articles

Final Thoughts

In this Azure article, we discussed, Azure Function Environment Variable. Thanks for reading this article !!!