AWS Lambda Bundler
aws-lambda-bundler is a JavaScript utility designed to create zip archives for AWS Lambda function deployments. Its primary feature is the explicit exclusion of the `aws-sdk` from the bundled package, aiming to reduce the overall deployment size, as the SDK is often the largest component. The package is currently at version 1.0.12. Released over six years ago, it appears to be no longer actively maintained, with minimal updates or recent community activity. While it offers a simple command-line and programmatic interface for basic bundling, modern AWS Lambda deployment practices typically favor more advanced bundlers like `esbuild` (often integrated into AWS CDK's `NodejsFunction` or Serverless Framework) or `webpack` for their superior performance, tree-shaking capabilities, and better support for contemporary JavaScript features like TypeScript and ESM. These newer alternatives provide more granular control over dependencies and often result in smaller, more optimized bundles and improved cold start times.
Common errors
-
Error: Cannot find module 'aws-lambda-bundler'
cause The package is not installed in your project's `node_modules`.fixRun `npm install aws-lambda-bundler` or `yarn add aws-lambda-bundler` in your project directory. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` syntax within an ES Module (ESM) file, or the bundler itself does not correctly handle ESM input/output.fixEnsure your Lambda function code is written in CommonJS if using older tooling, or migrate to a modern bundler (like `esbuild`) that fully supports ESM input and output for Node.js Lambda functions. -
Error: EACCES: permission denied, open '/path/to/output.zip'
cause The user running the bundling command or script does not have write permissions to the specified output directory.fixEnsure you have appropriate write permissions for the target output directory, or specify an output path where the user has permissions (e.g., a subdirectory within your project or a temporary directory).
Warnings
- breaking The package `aws-lambda-bundler` has not been updated in over six years and is considered abandoned. It may contain unpatched security vulnerabilities, lack support for modern Node.js runtimes (e.g., Node.js 18+), and not support contemporary JavaScript features or module systems (ESM). Using it in new projects is highly discouraged.
- gotcha This bundler's primary optimization is to exclude the `aws-sdk` to reduce bundle size. While this was a common practice, newer AWS Lambda runtimes (Node.js 18+) include AWS SDK v3, and for optimal cold start performance and full control, it is often recommended to bundle the specific AWS SDK v3 modules your function uses with your deployment package, rather than relying on the runtime's provided SDK.
- gotcha This package might not correctly handle advanced bundling scenarios such as TypeScript transpilation, ES Modules (ESM) resolution, or robust tree-shaking for complex dependency graphs. Its 'simple' approach of just zipping and excluding `aws-sdk` may lead to larger-than-necessary bundles or runtime errors with modern codebases.
Install
-
npm install aws-lambda-bundler -
yarn add aws-lambda-bundler -
pnpm add aws-lambda-bundler
Imports
- bundle
const bundle = require('aws-lambda-bundler').bundle;import { bundle } from 'aws-lambda-bundler'; - default export
import { default as bundler } from 'aws-lambda-bundler';import bundler from 'aws-lambda-bundler';
Quickstart
const { bundle } = require('aws-lambda-bundler');
const fs = require('fs');
const path = require('path');
async function createDummyLambdaProject() {
const projectDir = path.join(__dirname, 'my-lambda-project');
const handlerDir = path.join(projectDir, 'src');
const handlerFile = path.join(handlerDir, 'handler.js');
const packageJsonFile = path.join(projectDir, 'package.json');
if (!fs.existsSync(handlerDir)) {
fs.mkdirSync(handlerDir, { recursive: true });
}
fs.writeFileSync(handlerFile, `
exports.handler = async (event) => {
console.log('Received event:', event);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
`);
fs.writeFileSync(packageJsonFile, `
{
"name": "my-lambda-project",
"version": "1.0.0",
"description": "A dummy lambda for bundling demo",
"main": "src/handler.js",
"dependencies": {
"lodash": "^4.17.21"
}
}
`);
console.log('Dummy lambda project created at:', projectDir);
return projectDir;
}
async function runBundler() {
const projectPath = await createDummyLambdaProject();
try {
console.log(`\nBundling project at ${projectPath}...`);
// The bundle function takes the source directory and optionally an output path.
// It returns a promise that resolves with the path to the created zip file.
const zipFilePath = await bundle(projectPath);
console.log(`Successfully bundled to: ${zipFilePath}`);
console.log('You can now inspect the contents of the zip file.');
} catch (error) {
console.error('Bundling failed:', error);
process.exit(1);
} finally {
// Clean up dummy project after bundling
fs.rmSync(projectPath, { recursive: true, force: true });
console.log(`Cleaned up ${projectPath}`);
}
}
runBundler();