using environment variables from AWS in a nodeJS project - node.js

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.

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.

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

Strider CD can't access environment variables in nodejs

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

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