2.0.2 • Published 10 months ago

aws-lambda-event-handler v2.0.2

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
10 months ago

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

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago