@spreaker/lambda-powertools
TypeScript icon, indicating that this package has built-in type declarations

6.1.2 • Public • Published

Spreaker Lambda Powertools

Lambda Powertools is a package encapsulating utilities and best practices used to write Node.js Lambda functions at Spreaker.

Local development

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

Install package

npm install @spreaker/lambda-powertools

Components

PowerHandler

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" };
});

Logger

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

Throttling (optional)

The logger supports throttling that works like this:

  • If the log level of the logger is DEBUG

    • Logger will log 100% of DEBUG
    • Logger will log 100% of INFO
    • Logger will log 100% of WARN
    • Logger will log 100% of ERROR
  • For all the other log levels:

    • Logger will log 0% of DEBUG
    • Logger will log 10% of INFO
    • Logger will log 20% of WARN
    • Logger will log 100% of ERROR

The throttling, disabled by default, can be enabled setting to true (True, true, 1 are all valid values) the environment variable LOG_THROTTLE_ENABLED.

Example

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);
}

Network

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");

SQS

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.

Functions Overview

createSQSMessageBatches

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.

parseSNSRecordWithS3Event

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.

parseSQSRecordWithEventBridgeEvent

This function parses an SQS record containing an EventBridge event. It extracts the detail object from the record.

parseSQSRecordWithS3Event

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.

powerHandler

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

validateAndParseSqsRecord

This function validates and parses the body of an SQS record using a validator function.

validateMandatoryFieldsAndParseSQSRecord

This function validates and parses the payload of an SQS record, ensuring that it contains mandatory fields.

validateAndParseSqsInputEventWithMultipleRecords

This function validates and parses an SQS input event containing multiple records.

Readme

Keywords

none

Package Sidebar

Install

npm i @spreaker/lambda-powertools

Weekly Downloads

82

Version

6.1.2

License

ISC

Unpacked Size

35.2 kB

Total Files

15

Last publish

Collaborators

  • wanzemljak
  • save91
  • nonno
  • spreaker-owner
  • roccozanni
  • teo310182
  • lele85