AWS Lambda Function Builder
`aws-lambda-build` is a lightweight command-line interface (CLI) tool designed for building and packaging AWS Lambda functions into `.zip` archives. Its core functionality involves navigating to a specified function directory, executing `npm install` to resolve dependencies, and then zipping the entire directory contents into a deployable `.zip` file. Currently at version 1.0.8, the project has seen slow but stable development, with recent updates focusing on cross-platform compatibility across Windows, Mac, and Linux, and a transition to pure Node.js for its implementation (v1.0.4). A key differentiator is its minimal footprint, explicitly stating "no external dependencies except *archiver*", making it a highly self-contained solution compared to more complex build systems.
Common errors
-
lambda-build: command not found
cause The `aws-lambda-build` executable is not in the system's PATH, typically because the package was not installed globally or `npx` was not used.fixInstall the package globally: `npm install -g aws-lambda-build`, or execute it via `npx`: `npx aws-lambda-build [arguments]`. -
Error: Function directory not found: [your/path]
cause The path provided to the `--function` (or `-f`) argument does not point to an existing directory containing your Lambda function code.fixVerify that the `--function` path is correct and absolute, or relative to the directory where you are executing `lambda-build`. -
npm ERR! code ENOSPC
cause During the internal `npm install` step, the system ran out of disk space, preventing dependencies from being fully installed or extracted.fixFree up disk space on the machine where the build process is running.
Warnings
- gotcha This package is primarily a command-line interface (CLI) tool and is not designed for programmatic import as a JavaScript or TypeScript library. Its core functionality is exposed exclusively via the `lambda-build` executable.
- gotcha The `npm install` step performed by `aws-lambda-build` requires Node.js (>=5.0) and npm to be correctly installed and available in the system's PATH where the build command is executed.
- gotcha Although the tool claims cross-platform support (since v1.0.7), inconsistencies in path separators (e.g., backslashes on Windows vs. forward slashes on Linux/macOS) can still lead to errors if not handled carefully, especially when passing paths via command-line arguments.
Install
-
npm install aws-lambda-build -
yarn add aws-lambda-build -
pnpm add aws-lambda-build
Imports
- lambda-build
import { lambdaBuild } from 'aws-lambda-build'npx lambda-build --function=path/to/your/lambda --archive=output.zip
- Function Path Argument
lambda-build path/to/your/function
lambda-build -f path/to/your/function
- Archive Name Argument
lambda-build --function=... -n my-function.zip
lambda-build --function=... --archive=my-function.zip
Quickstart
const { exec } = require('child_process');
const path = require('path');
// Create a dummy Lambda function directory for demonstration
const functionName = 'MyTestLambda';
const functionPath = path.resolve(__dirname, functionName);
require('fs').mkdirSync(functionPath, { recursive: true });
require('fs').writeFileSync(path.join(functionPath, 'index.js'), `
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
return { statusCode: 200, body: JSON.stringify('Hello from ${functionName}!') };
};
`);
require('fs').writeFileSync(path.join(functionPath, 'package.json'), `
{
"name": "${functionName.toLowerCase()}",
"version": "1.0.0",
"description": "A simple Lambda function",
"main": "index.js",
"dependencies": {
"lodash": "^4.17.21"
}
}
`);
const archiveName = `${functionName}.zip`;
console.log(`Building Lambda function from: ${functionPath}`);
console.log(`Output archive: ${archiveName}`);
// Execute the aws-lambda-build CLI tool
// Using 'npx' ensures the tool is available without global installation.
exec(`npx aws-lambda-build --function=${functionPath} --archive=${archiveName} --verbose`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log('Lambda build completed successfully!');
console.log(`You can now upload ${archiveName} to AWS Lambda.`);
// Clean up the dummy function directory and zip file
require('fs').rmSync(functionPath, { recursive: true, force: true });
require('fs').rmSync(archiveName, { force: true });
console.log('Cleaned up dummy function and archive.');
});