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

903 Views Asked by At

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

2

There are 2 best solutions below

0
Desmond On BEST ANSWER

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.

1
Tarun Krishna On

I have faced similar issue with connection string being empty when deployed to azure.

I have fixed this issue by pasting the the connection string of storage account into the application Setting as below :

I have clicked on connection string name and then pasted the connection string over there as below.

enter image description here

Then when I set the value in portal, it worked for me, try to do this way.