How environment variables are used when deployed on aws - node.js

I am building a web application that utilises environment variables locally, and I want to put this on production (online). I am trying to find out how to set environment variables on AWS.
This is a node.js application and I am using elastic beanstalk on AWS for deployment. I have looked through https://docs.aws.amazon.com/cloud9/latest/user-guide/env-vars.html#env-vars-env-list , but I'm unsure of which option applies to me.
The .env file I have locally contains lines like
PASSWORD=MYPASSWORD
and I am using the dotenv package, with require('dotenv').config(); in the appropriate files. I am accessing environment variables in my code through things like process.env.PASSWORD (using the aforementioned example of a line in the .env file).
I've tried searching several places and am presented with various options, but I'm not sure which one applies to my environment variables.

The link you are following may help you in ec2 machine that is mangages by you, but if you are working Elasticbeanstalk I will recommend using Environment variable configuration provided by elasticbeanstalk.
I am not in favour of .env in case of Elasticbeanstalk, ECS and many other services where AWS provide easy and out of the box feature to set environment variable in the configuration and .env write environment to file which is less secure then system environment variable.
The interesting part of Elasticbeanstalk ENV is, the system environment variable has higher periphery then .env environment variable but better to not place dotenv on elasticbeanstalk.
Environment Properties and Other Software Settings
You can use environment properties to pass secrets, endpoints, debug
settings, and other information to your application. Environment
properties help you run your application in multiple environments for
different purposes, such as development, testing, staging, and
production.
elasticbeanstalk-deploy_nodejs
Example .ebextensions/options.config
option_settings:
aws:elasticbeanstalk:application:environment:
API_ENDPOINT: www.example.com/api
Now all you need
var endpoint = process.env.API_ENDPOINT
Environment Properties
The Environment Properties section lets you specify environment
configuration settings on the Amazon EC2 instances that are running
your application. These settings are passed in as key-value pairs to
the application.
Inside the Node.js environment running in AWS Elastic Beanstalk, you
can access the environment variables using process.env.ENV_VARIABLE
similar to the following example.
var endpoint = process.env.API_ENDPOINT

Related

Deploy Dockerized NodeJS application with the respective .env files

I am working on a NodeJS application which is a containerized application. I use Jenkins to build and deploy it. I have an environment .env file and along with this, also have .env files based on environments.
For example: .env.DEV, .env.SQA, .env.STG and .env.PROD.
Each file has different values for the variables based on environments.
When I am deploying my application, it always fetches variables from the .env file instead of specific environment file i.e. .env.DEV (if deploying on DEV server).
How do we use specific environment file while doing the deployment on Jenkins?
Note - I followed this great content on dotenv library but I didn't find anything helpful for my use-case. I even Googled a lot but didn't find much on this. Any help would be greatly appreciated.
You can use dotenv-flow which does exactly this, given the value of NODE_ENV environment value it will load the expected environment.
You also will need to make sure that the container receives the proper environment values from jenkins, this might help.

using environment variables from AWS in a nodeJS project

I have created a pipeline on AWS to deploy a nodeJS project from source (codecommit). I use codebuild to build the project and codedeploy to deploy it. Everything works but I need environment variables in my nodejs code.
I have the following code:
console.log(process.env.CUSTOM_VARIABLE);
when I add "CUSTOM_VARIABLE" as an environment variable to the codebuild project, it doesn't work.
I found this link: https://blog.shikisoft.com/define-environment-vars-aws-codebuild-buildspec/
I'm guessing in my nodeJS-code, I don't need "process.env" but I need to access the environment variable another way. Does someone knows how?
Partial solution (for now):
I have ec2 instances (dev and staging). I set the environment variable directly on the server with export ENVIRONMENT=dev. This works.

How to manage .env configuration in continous integration

I develop an application with nodejs and react. I use dotenv for configuration in my different environment.
I use TFS 2017 for build and release my application.
What is the best practise for add my .env file of production environment?
Production configs can be difficult to maintain. You can use any module or even write your own custom module to load environment variable in to your application.
However, maintaining production .env files locally for each product (i.e. committing them or putting them inside your docker image is a bad idea. If you ever happen to change some of those configs, you will have to manually change in the code for each application, build docker image (if required) and redeploy. If there are just a couple of applications, it might be easy. But if the number of related applications grow in size (as usually happens in case of microservice based architecture) - all of them sharing credentials, ips for database connections etc. it becomes a hectic task to manually change code and redeploy all the applications.
Usually, developers tend to keep a central repository of all credentials and environment variables. For examples, AWS offers parameter store and MS Azure offers Azure Key vault. In such a case, all the parameters are fetched during start time, and all we have to do is restart the application.
So usually people set only one global varibale NODE_ENV (as prod or dev), dynamically fetch all environment variables based on NODE_ENV running something like env $(node read-env-variables.js) node app.js.

AWS Elastic Beanstalk environment variables overriding at the EB application level

I am currently running the same NodeJS Sails app on two different "environments" within the same Elastic Beanstalk "application" (AWS-specific terms in quotes). My intention was for these "environments" to serve as staging and production. Sails provides a built-in method for swapping configs based on the environment variable NODE_ENV. However, when I set the environment variable in one "environment," it overrides the value for the other.
I have tried using the AWS CLI command eb setenv NODE_ENV="environment_name" as well as using the web interface. (The setting in question is at Application > Environment > Configuration > Software Configuration under the "Environment Properties" header.)
I have been trying to search for this issue, but I have been having a hard time finding anything that addresses it specifically. Answers like the selected one here imply that it is possible to do what I am looking for.
Is there something wrong with the way I am setting this variable? Am I misunderstanding how Elastic Beanstalk handles these settings? I understand that I can also set environment variables by changing the .ebextensions file for different deploys, but I would prefer a cleaner fix.
As requested, here is the output from eb status and eb list (actual application name replaced):
$ eb status
Environment details for: app-staging
Application name: app
Region: us-east-1
Deployed Version: 1.1.0
Environment ID: e-fxxxxxxxxx
Platform: 64bit Amazon Linux 2015.03 v2.0.1 running Node.js
Tier: WebServer-Standard
CNAME: app-staging.elasticbeanstalk.com
Updated: 2015-12-08 03:54:37.894000+00:00
Status: Ready
Health: Green
$ eb list
app-production
* app-staging
It turns out this issue was happening because the AWS web interface was not keeping up with the changes I was making. The environment variables were not being overridden. Logging out and back in fixed this for me.
I have noticed that, in general, many perceived issues on AWS may be fixed in this way.

Configure application on Google Cloud Platform with environment variables

I'm a newbie with Google Cloud App Engine and I want to deploy my simple Node.js application. I need to configure it for "production" environment. For example, I want to provide some credentials for email box, which my application is going to query for new emails.
I like to use nconf npm package, it gives me powerful and flexible configuration via command-line arguments or environment variables. I don't need to hard-code possibly secure values in source code.
Is there a way to configure environment variables or command-line arguments for an application in Google Developer Console?
P. S.
I found from documentation, that one can inline environment variables in app.yml config file:
env_variables:
FOO: 'myapp.bar'
But this is still not suitable for me, since app.yml is staged in git.

Resources