How to read environment variables in AWS lambda function in Node.js ?
To read the environment variables defined in the lambda function’s configuration, you can use
process.env.VARIABLE_NAME
All Environment variables defined in the lambda function configuration will be available in the process.env
.
Reading Configuration from .env file
Now instead of defining in the lambda function configuration, if you are defining it in a separate enviroment file like .env
you need to load it to the environment.
For this you need to install a npm module.
npm install dotenv --save
And then in your lambda function, use it to load the configurations from .env
file to process.env
.
require('dotenv').config()
console.log(process.env) // ## configuration from .env should be available here