Amazon OpsWorks NodeJS environment variables - node.js

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.

Related

Getting environment variables from nodejs app cpanel

So I'm using cPanel with Setup Node.js App plugin for an node/express app. (don't asky why cPanel)
Everything is working as expected in development, except for environment variables in production, I set up them manually from the cPanel interface, I restarted/stopped the app, logging the process.env on the server and I don't see the env variables there (not to say when trying to call them they are undefined).
When doing res.json(JSON.stringify(process.env)); i get a bunch of variables except for the one I manually wrote in cPanel variables interface.
It is important for me to store these variables as a secret key because they are API credentials.
Does anyone know what I might have misconfigured or had this problem?

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.
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

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.

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.

Retrieve local and heroku application hostname outside a request

I have to configure one of the module in my application and give it a specific URL, based on the hostname. Something like http://localhost locally and http://myapp.herokuapp.com. I know I can just hardcode the value depending on NODE_ENV, or even store it as an environment value and use process.env.CUSTOMVAR_HOSTNAME. But somehow it feels wrong, since myapp is already configured in the Heroku admin panel. And I can't use request.headers.host since the module configuration happens at the start of the application, before any request can be handled.
Is there a way to retrieve the Heroku application name from the code ? Or from an environment variable ? I want my code to stay the same whether I'm executing code locally or on Heroku (or any other place actually).
I didn't find any way, so I created a local variable hostname based on NODE_ENV (development or production) with the proper hostname value.

Resources