'How do i use azure app service env variables inside my application?

i'm trying to setup my project in azure. For this i created an app service and under settings/configuration i added some "Application Settings", which in my understanding are environment variables. However i deployed my docker image which azure pipelines and everything is okay, but when i connect through ssh to the instance and call 'env' i don't see any of my environment variables.

From the documentation i should be able to call them inside php as every other env variable (getenv..). Maybe i miss something or my understanding of this app settings are incorrect.

Would be great if somebody has an idea about what is wrong, if you need more informations hit me up.



Solution 1:[1]

Partly, you did not make mistake on the way of accessing setting/configuration which set as environment variable. The getenv() is correct.

when i connect through ssh to the instance and call 'env' i don't see any of my environment variables

I think this issue may caused by your script. When you access these setting keys, do not lost Prefixed. This is the important way to access and get these environment variable. For example, if you want to access app settings, the name of the corresponding environment variable should prepended with APPSETTING_.

At this time, the sample script for PHP script should be:

<?php 
  $appsetting = getenv('APPSETTING_{Key}'); echo $appsetting;
?>

Note: The {key} is the key name you configured in Azure app service.

For the configuration which under Connection Strings, it should be added with other prefixed. As you know, when you create these connection strings, you need to choose Type:

enter image description here

For these setting, the connection strings are available as environment variables, prefixed with the following connection types:

SQL Server: SQLCONNSTR_

MySQL: MYSQLCONNSTR_

SQL Database: SQLAZURECONNSTR_

Custom: CUSTOMCONNSTR_

For more details, check this doc: https://docs.microsoft.com/en-us/azure/app-service/configure-common#configure-connection-strings

Solution 2:[2]

I found this article which explains a little bit more how the whole stuff works.

https://omgdebugging.com/2018/10/05/how-to-export-environment-variables-in-azure-web-app-for-containers/

However i sticked to this article and used the provided workaround. If somebody knows a better solution, please let me know

Solution 3:[3]

Setting Environment Variables

  1. In Azure Portal, locate your app service
  2. On the left pane, click Configuration
  3. Under Application settings, click "New application setting"
  4. Fill in the name and value for the environment variable
  5. Click "OK", then at the top, click "Save"

Accessing Environment Variables

With PHP

Replace ENV_VAR_NAME with your own environment variable name.

$var = getenv('ENV_VAR_NAME');

With CLI - Linux

You can use your preferred SSH client, but in this case I'll use the one in Azure Portal.

  1. In Azure Portal, locate your app service
  2. On the left pane, click "SSH"
  3. Click "Go"
  4. Type printenv ENV_VAR_NAME, replace ENV_VAR_NAME with your environment variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mengdi Liang
Solution 2 Zero
Solution 3 Jenga