dazn-lambda-powertools
is a collection of middlewares, AWS clients and helper libraries that make working with lambda easier.
Writing Lambdas often involves the bootstrapping of specific tooling, like reading and forwarding on correlation-id's, emitting logs on a lambda timeout, and more.
Re-writing and maintaining this bootstrapping logic into every individual lambda can be a pain, so to prevent this re-work we created dazn-lambda-powertools
.
The quickest way to get setup is to use the opinionated pattern basic package, like so:
const wrap = require('@dazn/lambda-powertools-pattern-basic')
module.exports.handler = wrap(async (event, context) => {
return 42
})
For more control, you can pick and choose from the individual packages.
All of the powertool middlewares use the middy library, and therefore adhere to the middy API.
However, the other tools such as the clients are generic.
An integrated suite of powertools for Lambda functions that reduces the effort to implement common lamdba tasks, such as dealing with correlation-ids.
-
support correlation IDs
-
debug logs are turned off in production, and are instead sampled for 1% of invocations
-
debug logging decisions are respected by all the functions on a call chain
-
HTTP requests always report both latency as well as response count metrics
-
logger: structured logging with JSON, configurable log levels, and integrates with other tools to support correlation IDs and sampling (only enable debug logs on 1% of invocations)
-
correlation IDs: create and store correlation IDs that follow the DAZN naming convention
-
correlation IDs middleware: automatically extract correlation IDs from the invocation event
-
sample logging middleware: enable debug logging for 1% of invocations, or when upstream caller has made the decision to enable debug logging
-
obfuscater middleware: allows you to obfuscate the invocation event so that sensitive data (e.g. PII) is not logged accidentally
-
log timeout middleware: logs an error message when a function invocation times out
-
stop infinite loop middleware: stops infinite loops
-
http client: HTTP client that automatically forwards any correlation IDs you have captured or created, and records both latency as well as response count metrics
-
CloudWatchEvents client: CloudWatchEvents client that automatically forwards any correlation IDs you have captured or created when you put events to an event bus
-
EventBridge client: EventBridge client that automatically forwards any correlation IDs you have captured or created when you put events to an event bus
-
SNS client: SNS client that automatically forwards any correlation IDs you have captured or created when you publish a message to SNS
-
SQS client: SQS client that automatically forwards any correlation IDs you have captured or created when you publish a message to SQS
-
Kinesis client: Kinesis client that automatically forwards any correlation IDs you have captured or created when you publish record(s) to a Kinesis stream
-
Firehose client: Firehose client that automatically forwards any correlation IDs you have captured or created when you publish record(s) to a Firehose delivery stream
-
Step Functions client: Step Functions client that automatically forwards any correlation IDs you have captured or created when you start an execution
-
Lambda client: Lambda client that automatically forwards any correlation IDs you have captured or created when you invokes a Lambda function directly
-
DynamoDB client: DynamoDB client that automatically forwards any correlation IDs you have captured or created when you perform put or update operations against DynamoDB. These correlation IDs are then available to functions processing these events from the table's DynamoDB Stream.
-
basic template for a function: wrapper for your function that applies and configures the function to work well with datadog metrics and sample logging
-
obfuscate template: basic template (above) + obfuscate the invocation event so sensitive data is obfuscated in the
after
andonError
handlers.
Package | Install command |
---|---|
cloudwatchevents-client | npm install @dazn/lambda-powertools-cloudwatchevents-client |
correlation-ids | npm install @dazn/lambda-powertools-correlation-ids |
dynamodb-client | npm install @dazn/lambda-powertools-dynamodb-client |
eventbridge-client | npm install @dazn/lambda-powertools-eventbridge-client |
firehose-client | npm install @dazn/lambda-powertools-firehose-client |
http-client | npm install @dazn/lambda-powertools-http-client |
kinesis-client | npm install @dazn/lambda-powertools-kinesis-client |
lambda-client | npm install @dazn/lambda-powertools-lambda-client |
logger | npm install @dazn/lambda-powertools-logger |
middleware-correlation-ids | npm install @dazn/lambda-powertools-middleware-correlation-ids |
middleware-log-timeout | npm install @dazn/lambda-powertools-middleware-log-timeout |
middleware-obfuscater | npm install @dazn/lambda-powertools-middleware-obfuscater |
middleware-sample-logging | npm install @dazn/lambda-powertools-middleware-sample-logging |
middleware-stop-infinite-loop | npm install @dazn/lambda-powertools-middleware-stop-infinite-loop |
pattern-basic | npm install @dazn/lambda-powertools-pattern-basic |
pattern-obfuscate | npm install @dazn/lambda-powertools-pattern-obfuscate |
sns-client | npm install @dazn/lambda-powertools-sns-client |
sqs-client | npm install @dazn/lambda-powertools-sqs-client |
step-functions-client | npm install @dazn/lambda-powertools-step-functions-client |
You can also deploy the layer via our SAR app, which you can deploy either via this page (click Deploy
and follow the instructions) or using CloudFormation/Serverless framework/AWS SAM:
DaznLambdaPowertoolsLayer:
Type: AWS::Serverless::Application
Properties:
Location:
ApplicationId: arn:aws:serverlessrepo:us-east-1:570995107280:applications/dazn-lambda-powertools
SemanticVersion: <enter latest version>
and reference the output Outputs.LayerVersion
to get the ARN of the layer to reference in your function. e.g. Fn::GetAtt: [DaznLambdaPowertoolsLayer, Outputs.LayerVersion]
.
You can find the latest version of the SAR app in the lerna.json
file here, in the version
property.
Compliance with best practices around logging and monitoring should be the default behaviour. These tools make it simple for you to do the right thing and gets out of your way as much as possible.
Individually they are useful in their own right, but together they're so much more useful!
The middlewares capture incoming correlation IDs, and the logger automatically includes them in every log message, and the other clients (HTTP, Kinesis, SNS, etc.) would also automatically forward them on to external systems.
Even if your function doesn't do anything with correlation IDs, the tools make sure that it behaves correctly as these correlation IDs flow through it.
Instead of forcing you to use dazn-powertools AWS clients, we could have monkey patched the AWS SDK clients (which we already do in the tests). We could also monkey patch Node's http
module (like what Nock does) to intercept HTTP requests and inject correlation IDs as HTTP headers.
We could apply the monkey patching when you apply the correlation IDs middleware, and your function would "automagically" forward correlation IDs without having to use our own client libraries. That way, as a user of the tools, you could use whatever HTTP client you wish, and can use the standard SDK clients as well.
We did entertain this idea, but I wanted to leave at least one decision for you to make. The rationale is that when things go wrong (e.g. unhandled error, or bug in our wrapper code) or when they don't work as expected (e.g. you're using an AWS SDK client that we don't support yet), at least you have that one decision to start debugging (change the require
statement to use the official library instead of our own to see if things things still work).
Because of the inter-dependencies between packages, it can be tricky to test your changes haven't broken another package.
You can use Lerna CLI to bootstrap all the dependencies with the current local version:
lerna bootstrap
lerna run test
lerna create <name of package>
and follow the instruction to bootstrap the new project.
Please read our contribution guide to see how you can contribute towards this project.