Extract flame graph for Nodejs directly from an AWS lambda function. Lambda flame is a custom Nodejs runtime that collects and creates a flame graph of your lambda function process.
The first thing is to deploy the Serverless application into your account. You can do it via the AWS console: lambda-flame, or directly into Cloudformation: (make sure you specify the latest version)
lambdaflame:
Type: AWS::Serverless::ApplicationProperties:
Location:
ApplicationId: arn:aws:serverlessrepo:ap-southeast-1:164102481775:applications/lambda-flameSemanticVersion: 1.0.0After the application has been deployed go to the lambda console, click on layers and copy the lambda-flame layer arn;
You can now paste the layer in your SAM/Cloudformation template:
AWSTemplateFormatVersion: "2010-09-09"Transform: "AWS::Serverless-2016-10-31"Description: "Lambda flame test template"Resources:
ExampleFunction:
Type: AWS::Serverless::FunctionProperties:
Runtime: provided // Runtime must be set to providedTimeout: 30MemorySize: 1024CodeUri: ./Handler: handler.handlerEnvironment:
Variables:
LAMBDA_FLAME_DEST_BUCKET: !Ref FlameBucketLAMBDA_FLAME_DEBUG: ALLPolicies:
- AWSLambdaBasicExecutionRole
- S3WritePolicy: // Or simply s3:PutObjectBucketName: !Ref FlameBucketLayers:
- !Subarn:aws:lambda:ap-southeast-1:{AWS::AccountId}:layer:lambda-flame:13 // Layer arn from the Lambda Flame applicationEvents:
HTTP:
Type: ApiProperties:
Path: /Method: GetFlameBucket:
Type: AWS::S3::BucketProperties:
BucketName: lambda-flame-graph-testPaste the arn in your serverless.yaml:
service: lambda-flame-test-slsprovider:
name: awsstackName: lambda-flame-test-slsregion: ap-southeast-1iamRoleStatements:
- Effect: AllowAction:
- s3:PutObjectResource:
- arn:aws:s3:::lambda-flame-graph-test
- arn:aws:s3:::lambda-flame-graph-test/*package:
individually: truefunctions:
exampleFunction:
memorySize: 1024timeout: 30handler: handler.handlerruntime: providedenvironment:
LAMBDA_FLAME_DEST_BUCKET: lambda-flame-graph-testLAMBDA_FLAME_DEBUG: ALLlayers:
- !Subarn:aws:lambda:ap-southeast-1:${AWS::AccountId}:layer:lambda-flame:13events:
- http:
method: getpath: /resources:
Resources:
FlameBucket:
Type: AWS::S3::BucketProperties:
BucketName: lambda-flame-graph-testIf you are using the webpack plugin, the custom runtime option will not work for versions < 5.4.0
Just add the allowCustomRuntime: true option to your function:
exampleFunction:
handler: handler.handlerruntime: providedallowCustomRuntime: truelayers:
- !Subarn:aws:lambda:ap-southeast-1:${AWS::AccountId}:layer:lambda-flame:13...- The
Runtimeproperty must be set toprovided - All the result of the v8 profiling and the flamegraph creation will be sent to an S3 bucket of your choice. You must specify a valid S3 Bucket name.
You can use any bucket you want and one bucket for all your functions,
lambda flame will name space all the output files under
s3://<your-bucket-name>/lambda-flame/<function-name>/<epoch timestamp>/ - In order to send the profiling output and flame graph files to your S3 bucket make sure you have the required write permissions
s3:PutObject - The v8 profile output processing happens outside your handler invocation therefore it won't impact your function profiling. However, is a CPU bound operation, if you want to make it a bit faster just temporarily add more memory
- This tool (for the time being) is not meant to run in a production environment, use it as a debugging purposes only
After the invocation of your function has terminated, you can navigate to your S3 Bucket under s3://<your-bucket-name>/lambda-flame/<function-name>/<epoch timestamp>/
and open the flamegraph.html
You can set all the parameters via environmental variables
| Property | Type | Description | Default |
|---|---|---|---|
| LAMBDA_FLAME_DEST_BUCKET | String | Valid S3 bucket name required | undefined |
| LAMBDA_FLAME_DEBUG | String | Activate debugging logs, they will appear in Cloudwatch, leave it empty if you don't want them | ALL |
There are a lot of new features I want to add, feel free to contribute, open issues and share your ideas 🙏.
- Provide multiple runtimes other than nodejs (planning to support Go)
- Add more control over via configuration
- Automate builds of Nodejs runtimes
- Possibility to capture a heap dump
- Dramatically improve flame graph visualization and interactivity
This tool (for the time being) is partially using a modified version flamebearer all the credits goes to the author of this library. In the future I am planning to build a custom visualization similar to Clinic flame.


