Environment variable middleware for the middy framework
npm install middy-envThe specified environment variables will be parsed and passed into the handler.
cache(boolean) (optional): Set it totrueto skip further lookups of environment variables. Defaults tofalse.cacheExpiryInMillis(int) (optional): Time in milliseconds for values to remain cached. Defaults toundefined.setToContext(boolean) (optional): This will assign the parsed values to thecontextobject of the function handler rather than toprocess.env. Defaults totrue.names(object) (required): Map of environment variables to parse, where the key is the destination.
Either provide just the environment variable key. Or provide the key, type and fallback value e.g.['KEY', 'string', 'fallbackValue'].
By default parameters are assigned to the function handler's context object. They can instead be assigned to the Node.js process.env object by setting the setToContext flag to false.
If no fallback value is provided a ReferenceError will be thrown if an environment variable is undefined.
stringintfloatbool
const middy = require('middy');
const env = require('middy-env');
const handler = (event, context, callback) => {
callback(null, `Hello ${context.firstName} ${context.lastName}`);
};
module.exports = middy(handler)
.use(env({
names: {
firstName: ['FIRST_NAME', 'string', 'World'],
lastName: 'LAST_NAME'
},
cache: true,
cacheExpiryInMillis: 3600000
}));