{"library":"middy-profiler","title":"Middy AWS Lambda CPU Profiler","description":"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.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install middy-profiler"],"cli":null},"imports":["import profiler from 'middy-profiler';","const profiler = require('middy-profiler');","import middy from '@middy/core';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import middy from '@middy/core';\nimport profiler from 'middy-profiler';\n\n// A basic AWS Lambda handler function\nconst handler = async (event, context) => {\n  console.log('Lambda invoked with event:', event);\n  // Simulate some CPU-intensive work\n  let sum = 0;\n  for (let i = 0; i < 10000000; i++) {\n    sum += Math.sqrt(i);\n  }\n  console.log('Work done, sum:', sum);\n\n  return {\n    statusCode: 200,\n    body: JSON.stringify({ message: 'Profiling complete' }),\n  };\n};\n\n// Wrap the handler with middy and apply the profiler middleware\n// Ensure MIDDY_PROFILER_S3_BUCKET_NAME env var is set or pass it via options.\nexport const profiledHandler = middy(handler).use(\n  profiler({\n    s3: {\n      bucketName: process.env.MIDDY_PROFILER_S3_BUCKET_NAME ?? 'your-profiling-bucket-name' // Mandatory\n    },\n    fileName: 'my-function-profile' // Optional custom file name\n  })\n);\n\n// Example of how to enable coldstart profiling (requires setting NODE_OPTIONS env var outside of code)\n// process.env.NODE_OPTIONS = '--require /path/to/middy-profiler/bootstrap.js';\n","lang":"typescript","description":"Demonstrates how to integrate middy-profiler into an AWS Lambda handler using ESM, configure the S3 bucket for profile storage, and apply the middleware to an existing function.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}