Search code examples
javascriptnode.jsazure-functionsapplication-settings

Azure Function Node JS unable to read Application Settings returns undefined for process.env.variable_name


Application settings contain the Azure Storage Account Connection string. The function app is designed to generate the SAS token. Have set the Azure Storage Account connection string as part of the Application settings to the function app. When the application is hosted on Azure it returns undefined.

However, the same connection string which is stored in local.settings.json is able to read when running locally.

Bellow is the code to read the connection string in Azure Function

let connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
 context.log("context - Connection String ", connectionString);

This returns the correct connection string when running locally but not when published to server. Local.Settings.json code enter image description here

Function app Application Settings

enter image description here


Solution

  • The reason why the Function app couldn't read the Application Settings is that when the application was created using terraform's azurerm_linux_function_app module I failed to mention application_stack settings. However, the deployed code was able to function but it was not able to read the application settings. This is evident from the application settings configuration page in Azure Portal.

    enter image description here

    In the picture above we couldn't find the FUNCTIONS_WORKER_RUNTIME this settings will be placed by the Terraform if we have correctly specified its application_stack. Also, note that when the function app is deployed via Visual Studio code these are correctly specified. In the same way, when the Function is created via Azure Portal it is again set correctly.

    Bellow is the correct code for azurerm_linux_function_app for node function app.

    enter image description here

    After deploying the above code then the application settings are reflected as below.

    enter image description here

    So ensure that this value is correctly reflected when we can read value from process.env object.