middy-env is a middleware for the middy framework, specifically designed to parse and inject environment variables into AWS Lambda function handlers. It's currently at version 2.0.0, which aligns with the major version 2.x of the core middy framework, ensuring compatibility with the latest middy features and architecture. This package provides robust configuration management for serverless functions, allowing developers to define required environment variables, specify their expected types (string, int, float, bool), and provide fallback values to prevent runtime errors. A key differentiator is its ability to assign parsed variables directly to the Lambda `context` object by default, rather than polluting the global `process.env`, offering a cleaner and more isolated configuration scope per invocation. It also supports optional caching of parsed values to optimize performance for subsequent warm invocations, with configurable expiry times. The package maintains a stable release cadence, typically aligning with major middy framework updates.
npm install middy-envVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to apply the `middy-env` middleware to a Lambda handler, parsing `FIRST_NAME` and `LAST_NAME` environment variables and injecting them into the `context` object, with caching enabled.
Upgrade your middy framework to v2.x (`npm install @middy/core@^2`) or downgrade middy-env to v1.x (`npm install middy-env@^1`).
Always provide a fallback value in your `names` configuration (e.g., `['KEY', 'string', 'defaultValue']`) or guarantee the environment variable is always present in your deployment environment.
If you require the parsed variables to be available on `process.env`, set the `setToContext` option to `false` in your middleware configuration (e.g., `env({ names: { ... }, setToContext: false })`).Update your `middy-env` configuration to include a fallback value for `lastName` (e.g., `lastName: ['LAST_NAME', 'string', '']`) or ensure the `LAST_NAME` environment variable is correctly set in your Lambda function's environment.