Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

aws-lambda-event-handler

flamehamster23BSD-3-Clause2.0.2TypeScript support: included

Simple event handler for AWS Lambda

AWS, AWS Lambda, Lambda, Event Handling, SQS, SNS, MSK, EventBridge

readme

aws-lambda-event-handler

npm package npm license

Instead of using one Lambda function for each event, we can add multiple event triggers to a single Lambda function. aws-lambda-event-handler allows us to manage multiple events in one Lambda function easily.

Introduction

lambda.sns(topicArn: string, processSnsMessage: (message: SNSMessage) => Promise<void>): void
  • processSnsMessage is an async function that takes in SNSMessage.
lambda.sqs(queueArn: string, processSqsRecord: (record: SQSRecord) => Promise<void>): void
  • processSqsRecord is an async function that takes in SQSRecord.
  • Records in a batch are processed concurrently by processSqsRecord function.
  • Records processed successfully are removed from the queue.
  • Records processed unsuccessfully will remain in the queue.
  • Additional permission sqs:DeleteMessage is needed.
lambda.sqsFifo(queueArn: string, processSqsRecord: (record: SQSRecord) => Promise<void>): void
  • processSqsRecord is an async function that takes in SQSRecord.
  • Records in a batch are processed sequentially by processSqsRecord function.
  • Records processed successfully are removed from the queue.
  • When a record is processed unsuccessfully, subsequent records will remain in the queue.
  • Additional permission sqs:DeleteMessage is needed.
lambda.msk(mskArn: string, mskTopic: string, processMskRecord: (record: MSKRecord) => Promise<void>): void
  • processMskRecord is an async function that takes in MSKRecord.
  • Records in a batch are processed sequentially by processMskRecord function.
  • After Lambda processes each batch, it commits the offsets of the records in that batch.
  • If processMskRecord returns an error for any of the records in a batch, Lambda retries the entire batch of records.
lambda.scheduledEvent(ruleArn: string, processScheduledEvent: () => Promise<void>): void
  • processScheduledEvent is an async function that processes EventBridge (CloudWatch Events) scheduled message event.
  • More information here.

Installation

Note: AWS Lambda Node.js 18 runtime has AWS SDK for JavaScript v3.188.0.

JavaScript

$ npm i aws-lambda-event-handler

TypeScript

$ npm i aws-lambda-event-handler
$ npm i -D @aws-sdk/client-sqs@3.188.0
$ npm i -D @types/aws-lambda

Usage

JavaScript

const { Lambda } = require('aws-lambda-event-handler');

const lambda = new Lambda();

const processScheduledEvent = async () => {
    try {
        // do something...
    } catch (err) {
        // log error in cloudwatch
        console.error(err);
    }
};
lambda.scheduledEvent('<RULE_ARN>', processScheduledEvent);

const processSqsRecord = async (record) => {
    try {
        const jsonObject = JSON.parse(record.body);
        // do something...
    } catch (err) {
        // console.error(err); log error in cloudwatch to mark as successful
        // throw err; throw error to mark as unsuccessful
    }
};
lambda.sqs('<SQS_ARN>', processSqsRecord);
lambda.sqsFifo('<SQS_FIFO_ARN>', processSqsRecord);

const processSnsMessage = async (message) => {
    try {
        const jsonObject = JSON.parse(message.Message);
        // do something...
    } catch (err) {
        // console.error(err); log error in cloudwatch
        // throw err; throw error to trigger retry, if configured
    }
};
lambda.sns('<TOPIC_ARN>', processSnsMessage);

const processMskRecord = async (record) => {
    try {
        const value = Buffer.from(record.value, 'base64').toString();
        const jsonObject = JSON.parse(value);
        // do something...
    } catch (err) {
        // console.error(err); log error in cloudwatch
        // throw err; throw error to trigger retry, if configured
    }
};
lambda.msk('<MSK_ARN>', '<MSK_TOPIC>', processMskRecord);

exports.handler = lambda.handler;

TypeScript

import { Lambda } from 'aws-lambda-event-handler';
import { SQSRecord, SNSMessage, MSKRecord } from 'aws-lambda';

const lambda = new Lambda();

const processScheduledEvent = async (): Promise<void> => {
    try {
        // do something...
    } catch (err) {
        // log error in cloudwatch
        console.error(err);
    }
};
lambda.scheduledEvent('<RULE_ARN>', processScheduledEvent);

const processSqsRecord = async (record: SQSRecord): Promise<void> => {
    try {
        const jsonObject = JSON.parse(record.body);
        // do something...
    } catch (err) {
        // console.error(err); log error in cloudwatch to mark as successful
        // throw err; throw error to mark as unsuccessful
    }
};
lambda.sqs('<SQS_ARN>', processSqsRecord);
lambda.sqsFifo('<SQS_FIFO_ARN>', processSqsRecord);

const processSnsMessage = async (message: SNSMessage): Promise<void> => {
    try {
        const jsonObject = JSON.parse(message.Message);
        // do something...
    } catch (err) {
        // console.error(err); log error in cloudwatch
        // throw err; throw error to trigger retry, if configured
    }
};
lambda.sns('<TOPIC_ARN>', processSnsMessage);

const processMskRecord = async (record: MSKRecord): Promise<void> => {
    try {
        const value = Buffer.from(record.value, 'base64').toString();
        const jsonObject = JSON.parse(value);
        // do something...
    } catch (err) {
        // console.error(err); log error in cloudwatch
        // throw err; throw error to trigger retry, if configured
    }
};
lambda.msk('<MSK_ARN>', '<MSK_TOPIC>', processMskRecord);

const handler = lambda.handler;

export { handler };

changelog

Changelog

2.0.2

Update README

2.0.1

Use AWS SDK 3.188.0 for Node.js 18 Update Dependencies

1.1.2

Update Dependencies

1.1.1

Fix README

1.1.0

Add support to AWS MSK Events

Fix default import not working for JavaScript

  • New JavaScript import:
    • const { Lambda } = require('aws-lambda-event-handler');
  • New TypeScript import:
    • import { Lambda } from 'aws-lambda-event-handler';

1.0.4

First stable version