Strider CD can't access environment variables in nodejs - node.js

I'm using the strider-env plugin to create some environment variables for my application but I can't access them once it is deployed.
It is a Node.js application, I thought these variables would be added to process.env, but they aren't.
What am I missing here?
Thanks in advance

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 environment variables are used when deployed on aws

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

How to access Linux environment variables in angular 2?

I am trying to access a Linux environment variable in angular 2 but I am not sure how to do it. For example if I export a variable in terminal i can access it in node but I don't know how to access this variable in angular 2.
$ export HOST_IP="192.168.1.1"
I can access this environment variable in node like: process.env.HOST_IP. Could anyone please tell me how to access it in angular 2.
Thanks
NodeJS applications are run in a Server. So a NodeJS app can access the attributes of the server (Ex : Environmental variables).
But an Angular application runs on the browser. Thus it cannot access to the details of the machine that it is run on (there are some exceptions of course, but I'll not discuss them here).
Said that, if your true requirement is to change certain variables depending on the build of the Angular app (ex : test build, debug build, production deployment build), You can use the environment files. There are plenty of guidelines on the internet on how to do that. Few of them are,
https://alligator.io/angular/environment-variables/
https://medium.com/beautiful-angular/angular-2-and-environment-variables-59c57ba643be
https://medium.com/#natchiketa/angular-cli-and-os-environment-variables-4cfa3b849659

How to store database credentials for an open-source Heroku Node.js app?

I am building a Node.js application and need to store database credentials (and other runtime properties) in such a way that they can be read when deployed on Heroku. My source is available in a public GitHub repository.
I am currently using environment variables, configured using heroku config:add, but am looking to understand if there are any alternatives. I would potentially like to use Cloud9 IDE, but it does not currently support environment variables.
Another option is to store the parameters in a config. file, but I believe the file would need to be checked in to Git (and as such, be publicly available) in order to be pushed to Heroku.
Thanks for your help.
ENV vars are generally considered the way to go, and the way Heroku do it themselves for database_urls and the like.
As you and your app are the only people with access to the env vars, you're generally OK security wise.
Putting credentials in Git or similar is a bad idea as it's another place that needs to be secured.
The one way I know of to solve the problem for development using command-line arguments. These can be specified in your run/debug configuration. You can then access the parameters in process.argv. Of course this means that they will be stored in your Cloud9IDE dev environment. You could then use the ENV variables in a retail production. This will at least prevent the credentials from being visible in source or config files.

Resources