Middy AWS Lambda CPU Profiler
raw JSON →middy-profiler is an AWS Lambda middleware designed to capture CPU profiling data for Node.js functions, integrating seamlessly with the Middy framework. As of its current stable version 1.0.11, it enables developers to identify performance bottlenecks within their serverless functions by saving detailed CPU profiles to a specified AWS S3 bucket. These `.cpuprofile` files can then be analyzed using standard tools like Chrome DevTools for in-depth performance analysis. The package maintains a consistent release cadence with minor updates addressing features like conditional reporting, start delays, and coldstart profiling. Key differentiators include its tight integration with Middy, a standalone usage option, and the ability to profile module initialization during cold starts, which is crucial for optimizing initial function execution. Proper configuration of an S3 bucket and corresponding Lambda permissions are mandatory for its operation. It requires `@middy/core` version 2.0.0 or higher.
Common errors
error Profiling disabled: S3 bucket name is not specified. ↓
MIDDY_PROFILER_S3_BUCKET_NAME environment variable in your Lambda configuration or provide the bucket name directly in the middleware options: profiler({ s3: { bucketName: 'your-bucket' } }). error The specified bucket does not exist (Service: S3, Status Code: 404, Request ID: [...], S3 Extended Request ID: [...]) ↓
error Access Denied (Service: S3, Status Code: 403, Request ID: [...], S3 Extended Request ID: [...]) ↓
s3:PutObject permission to your Lambda function's IAM role for the target S3 bucket. Ensure the resource ARN matches your bucket and key prefix (e.g., arn:aws:s3:::your-bucket-name/*). error TypeError: middy is not a function ↓
@middy/core is installed (npm install @middy/core@^2) and imported correctly: import middy from '@middy/core'; for ESM or const middy = require('@middy/core'); for CommonJS. Warnings
gotcha The S3 bucket name for storing CPU profiling data is a mandatory configuration. If not specified via environment variable (`MIDDY_PROFILER_S3_BUCKET_NAME`) or middleware options, profiling will be silently disabled. ↓
gotcha Your AWS Lambda execution role must have sufficient S3 permissions (`s3:PutObject`) to write to the specified profiling bucket. Lack of permissions will result in failed profile uploads. ↓
gotcha To profile the module initialization phase (coldstart), you must configure the `NODE_OPTIONS` environment variable with the `bootstrap.js` file provided by `middy-profiler`. Standard middleware application only profiles from the request start. ↓
gotcha This package requires `@middy/core` version `2.0.0` or higher as a peer dependency. Using an older version of Middy may lead to compatibility issues or errors. ↓
Install
npm install middy-profiler yarn add middy-profiler pnpm add middy-profiler Imports
- profiler wrong
import { profiler } from 'middy-profiler';correctimport profiler from 'middy-profiler'; - profiler
const profiler = require('middy-profiler'); - middy wrong
import { middy } from '@middy/core';correctimport middy from '@middy/core';
Quickstart
import middy from '@middy/core';
import profiler from 'middy-profiler';
// A basic AWS Lambda handler function
const handler = async (event, context) => {
console.log('Lambda invoked with event:', event);
// Simulate some CPU-intensive work
let sum = 0;
for (let i = 0; i < 10000000; i++) {
sum += Math.sqrt(i);
}
console.log('Work done, sum:', sum);
return {
statusCode: 200,
body: JSON.stringify({ message: 'Profiling complete' }),
};
};
// Wrap the handler with middy and apply the profiler middleware
// Ensure MIDDY_PROFILER_S3_BUCKET_NAME env var is set or pass it via options.
export const profiledHandler = middy(handler).use(
profiler({
s3: {
bucketName: process.env.MIDDY_PROFILER_S3_BUCKET_NAME ?? 'your-profiling-bucket-name' // Mandatory
},
fileName: 'my-function-profile' // Optional custom file name
})
);
// Example of how to enable coldstart profiling (requires setting NODE_OPTIONS env var outside of code)
// process.env.NODE_OPTIONS = '--require /path/to/middy-profiler/bootstrap.js';