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

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.

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:
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://learn.microsoft.com/en-us/azure/app-service/configure-common#configure-connection-strings

Setting Environment Variables
In Azure Portal, locate your app service
On the left pane, click Configuration
Under Application settings, click "New application setting"
Fill in the name and value for the environment variable
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.
In Azure Portal, locate your app service
On the left pane, click "SSH"
Click "Go"
Type printenv ENV_VAR_NAME, replace ENV_VAR_NAME with your environment variable.

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

Related

AppServices won't allow ":" in setting name but needed for linux env var

I have published my linux container to App Services for Containers.
The way my container works is that it reads settings like API keys and connections strings from environment variables inside the running container.
I found a stack overflow post that says to set the env vars I need to use the "App Settings" in Azure. The problem is all the env vars have a colon in them, like:
database:connectionString=myConnectionString
App Services will not allow me to set a key that has a colon in it, however this is a perfectly valid syntax for a linux environment variable.
I really don't want to inject passwords during the build process into the actual image as that could cause a lot of issues.
How can I set an env var in App Services for containers that has a : in the key?
You could set your connection string, however : should be replaced by __(i.e. double underscore).You could find this from this doc. This principle is for Linux App.
Hope this could help you, if you still have other questions,please let me know.

azure function cannot read application setting using configurationManager or GetEnvironmentVariable

I've set some application settings in the azure function app but I'm getting null when I try to read them
System.ConfigurationManager.AppSettings[name]
System.Environment.GetEnvironmentVariable(variableKey, System.EnvironmentVariableTarget.Process)
Both of them gave me null. I can get the value fine running on local where my settings are in local.settings.json
(pulling the answers from the comments to aid in future searches)
When dealing with Appsettings, try restarting the app. (although normally the site would restart automatically when you change an appsetting)
You can use Kudu to verify the appsettings are indeed properly set on the site. (https://{app}.scm.azurewebsites.net) and click on Environment tab). They'll also show up as environment variables.

Node not able to read environment variables in AWS Beanstalk

I can't go into details unfortunately, but I'll try to be as thorough as possible. My company is using AWS Beanstalk to deploy one of our node services. We have an environment property through the AWS configuration dashborad, the key ENV_NAME pointing to the value in this case one of our domains.
According to the documentation, and another resource I found once you plug your variables in you should be able to access it through process.env.ENV_NAME. However, nothing is coming out. The names are correct, and even process.env is logging out an empty Object.
The documentation seems straight forward enough, and the other guide as well. Is anyone aware of any extra steps between setting the key value pair in the dashboard, and console logging out the value once the application is running in the browser?
Turns out I'm an idiot. We were referencing the environment variable in the JavaScript that was being sent to the client. So, we were never looking for the env variable until after it was off the server. We've added a new route to fetch this in response.

Amazon OpsWorks NodeJS environment variables

I configured a stack for a NodeJS application server using Amazon OpsWorks.
I need to access some environment variables which define Google API credentials. How can I achieve this ? I already spent more two days on this.
I ended up by the following chef recipe :
magic_shell_environment "GOOGLE_CLIENT_ID" do
owner 'root'
group 'root'
value "********"
mode '0600'
end
I use the root account because it seems the NodeJS is run under that account. If I remove the owner and group attributes, I can read those environment variables fine (as the default ubuntu user). However, when I ssh to my instance and type echo $GOOGLE_CLIENT_ID as root, I get an empty string.
Also, where is logged the output of console.xxxx(...) ?
OpsWorks now lets you specify up to 20 custom environment variables in the app settings page. In the case of a node.js app these will be available in the process.env object.
This should be fairly easy to do. Just add the following line to the top of your recipe.
ENV['GOOGLE_CLIENT_ID']="YOUR_CLIENT_ID"
Use the OpsWorksEnvy cookbook. It hooks nicely into the default nodejs cookbooks and lets you set the environment variables in your stack attributes.

TeamCity - Use environment variable in buildAgent.properties file?

I'm in the process of setting up TeamCity on Azure, more specifically I'm setting up some build agents. Certain properties of the build agents are configured via the buildAgent.properties file like name, port etc. I would like to call the agent the name of the computer it is located on but I can't find (and indeed don't know if this is possible) anyway to use environment variables to set the agent name i.e env.COMPUTERNAME?
The only way i seem to be able to do this is by hard coding the name
#Generated properties (see buildAgent.dist.properties for description)
name=tc-agent-001 #is it possible to use environment variable here?
ownPort=9090
serverUrl=http\://mybuildserver.mycompany.com
workDir=C\:\\BuildAgent\\work
tempDir=C\:\\BuildAgent\\temp
#User defined properties
env.TEAMCITY_JRE=C\:\\BuildAgent\\jre
Has anyone figured out a way to do this?
Our agents had this behaviour by default. It is the name property that appears in the env.COMPUTERNAME environment variable, and this was the hostname of each VM.
If your machine hostname is different to the listed name, just change that parameter, but as this file is just a parsed list of settings, there's not a way to have it programatically detect the hostname for you. - unless you write a script to detect the hostname of a machine and then modify the properties file.

Resources