Ad

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.

Ad

Answer

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
Ad
source: stackoverflow.com
Ad