Lambda Powertools is a package encapsulating utilities and best practices used to write Node.js Lambda functions at Spreaker.
To develop locally the library spin on the necessary containers:
docker-compose up --build
Enter the dev container and install dependencies:
docker-compose exec dev sh
npm install
Then you can:
- Run unit tests once with
npm run test
- Run unit tests in watch with
npm run test-watch
- Run the type checker with
npm run typecheck
npm install @spreaker/lambda-powertools
PowerHandler wraps a traditional Lambda handler and makes it smarter.
- Automatic intercept prometheus metrics generated during the lambda execution and serialize them into the logs so they can be ingested at a later stage. At the beginning of each execution the content of the Prometheus global registry is reset to avoid values from previous executions ending up summing with the newly generated ones.
Example:
const { powerHandler } = require("@spreaker/lambda-powertools");
const counter = new client.Counter({
name: "lambda_invocations_total",
help: "Total number of lambda invocations",
});
exports.example = powerHandler(async function (event, context) {
counter.inc();
return { foo: "bar" };
});
This logger provides an out-of-the-box logging experience compliant to Spreaker best practices:
- It use structured logging adhering to the Spreaker logging policies
- When used in conjunction with the PowerHandler it's automatically initialized to capture useful information from the execution environment, like the function version and memory, and the various ids injected by AWS based on the execution context
- It automatically enables debug logging for the incoming requests when specified
- Embedded throttling policy
The logger supports throttling that works like this:
-
If the log level of the logger is
DEBUG
- Logger will log
100%
ofDEBUG
- Logger will log
100%
ofINFO
- Logger will log
100%
ofWARN
- Logger will log
100%
ofERROR
- Logger will log
-
For all the other log levels:
- Logger will log
0%
ofDEBUG
- Logger will log
10%
ofINFO
- Logger will log
20%
ofWARN
- Logger will log
100%
ofERROR
- Logger will log
The throttling, disabled by default, can be enabled setting to true (True
, true
, 1
are all valid values) the environment variable LOG_THROTTLE_ENABLED
.
const { logger } = require("@spreaker/lambda-powertools");
// Levels
logger.debug("This is debug");
logger.info("This is info");
logger.warn("This is warn");
logger.error("This is error");
// Additional context
logger.info("This is info", { foo: "bar" });
// Log errors
try {
throw new Error("Error message");
} catch (err) {
logger.error("This is error", err);
}
A utility module containing methods needed in networking.
const { network } = require("@spreaker/lambda-powertools");
// this method return the ip (possibly converted to IPv4) and the IP type
network.getIpWithType("1.1.1.1");
{ "ip": "1.1.1.1", "type": "IPv4" }
// this method returns the canonical form of the IPv6 passed, or null otherwise
network.IPv6ToCanonicalForm("2001:db8::1:0:0:1");
("2001:0db8:0000:0000:0001:0000:0000:0001");
This module contains functions for processing AWS Lambda SQS events. These functions are designed to handle various types of SQS records and perform operations such as parsing, validation, and batch processing.
This function takes an array of messages and returns an array of message batches suitable for sending to SQS using the sendMessageBatch function. It ensures that each batch does not exceed the maximum size limit and the maximum number of entries per batch.
This function parses an SQS record containing an S3 event from an SNS message. It extracts information such as event time, bucket name, and object key from the record.
This function parses an SQS record containing an EventBridge event. It extracts the detail object from the record.
This function parses an SQS record containing an S3 event. It extracts information such as event time, bucket name, and object key from the record.
This function is a power handler for processing SQS events. It can operate in two modes: parallel or merge. In parallel mode, it processes one record at a time in parallel, while in merge mode, it merges records and processes them together.
- Parallel Mode:
powerTools.sqs.powerHandler(
async (record) => {
if (error) {
throw new Error("some_error");
}
return;
},
{
mode: "parallel",
limit: 10,
}
);
- Merge Mode:
powerTools.sqs.powerHandler(
async (records) => {
return { batchItemFailures: [] };
},
{ mode: "merge" }
);
Important: You have to add functionResponseType: ReportBatchItemFailures
in serverless.yaml
for each handler that is wrapped with powerTools.sqs.powerHandler
This function validates and parses the body of an SQS record using a validator function.
This function validates and parses the payload of an SQS record, ensuring that it contains mandatory fields.
This function validates and parses an SQS input event containing multiple records.