Adapters
Adapters help integrate the workflow engine with different runtime environments.
Installation
import { LambdaEventHandler } from 'nestjs-serverless-workflow/adapter';
Lambda Adapter
The Lambda adapter enables running workflows in AWS Lambda with SQS event sources.
Setup
- Install AWS Lambda types:
npm install -D @types/aws-lambda
- Create a Lambda handler:
import { NestFactory } from '@nestjs/core';
import { LambdaEventHandler } from 'nestjs-serverless-workflow/adapter';
import { type SQSHandler } from 'aws-lambda';
import { AppModule } from './app.module';
// Initialize NestJS application
const app = await NestFactory.createApplicationContext(AppModule);
await app.init();
// Export Lambda handler
export const handler: SQSHandler = LambdaEventHandler(app);
Features
Automatic Timeout Handling
The Lambda adapter automatically manages Lambda timeouts:
- Tracks remaining execution time
- Gracefully stops processing before timeout
- Returns unprocessed messages for retry
// Safety window: stops 5 seconds before Lambda timeout
const safetyWindowMs = context.getRemainingTimeInMillis() - 5000;
Batch Item Failures
Supports partial batch failures for efficient retry:
// Failed messages are automatically marked for retry
return {
batchItemFailures: [
{ itemIdentifier: 'message-id-1' },
{ itemIdentifier: 'message-id-2' },
],
};
Configuration
SQS Event Source Mapping
Configure your Lambda function with SQS:
# serverless.yml
functions:
workflowHandler:
handler: dist/lambda.handler
events:
- sqs:
arn: !GetAtt WorkflowQueue.Arn
batchSize: 10
functionResponseType: ReportBatchItemFailures
timeout: 300
Important: Set functionResponseType: ReportBatchItemFailures to enable partial batch failures.
Lambda Timeout
Set appropriate timeouts based on your workflow complexity:
functions:
workflowHandler:
timeout: 300 # 5 minutes
The adapter will stop processing 5 seconds before this timeout to ensure graceful shutdown.
Example Lambda Handler
import { NestFactory } from '@nestjs/core';
import { LambdaEventHandler } from 'nestjs-serverless-workflow/adapter';
import { type SQSHandler } from 'aws-lambda';
import { WorkflowModule } from 'nestjs-serverless-workflow/core';
import { OrderWorkflow } from './order.workflow';
import { OrderEntityService } from './order-entity.service';
import { MySqsEmitter } from './sqs.emitter';
// Create NestJS application context
const app = await NestFactory.createApplicationContext(
WorkflowModule.register({
entities: [
{ provide: 'entity.order', useClass: OrderEntityService },
],
workflows: [OrderWorkflow],
brokers: [
{ provide: 'broker.order', useClass: MySqsEmitter },
],
})
);
await app.init();
// Export handler
export const handler: SQSHandler = LambdaEventHandler(app);
Creating Custom Adapters
You can create adapters for other runtimes:
HTTP Adapter Example
import { Controller, Post, Body } from '@nestjs/common';
import { OrchestratorService } from 'nestjs-serverless-workflow/core';
import { IWorkflowEvent } from 'nestjs-serverless-workflow/event-bus';
@Controller('workflow')
export class WorkflowController {
constructor(private orchestrator: OrchestratorService) {}
@Post('events')
async handleEvent(@Body() event: IWorkflowEvent) {
await this.orchestrator.transit(event);
return { status: 'processed' };
}
}
EventBridge Adapter Example
import { EventBridgeHandler } from 'aws-lambda';
import { OrchestratorService } from 'nestjs-serverless-workflow/core';
export const handler: EventBridgeHandler<string, any, void> = async (event) => {
const app = await getApp();
const orchestrator = app.get(OrchestratorService);
const workflowEvent = {
topic: event['detail-type'],
urn: event.detail.entityId,
payload: event.detail,
attempt: 0,
};
await orchestrator.transit(workflowEvent);
};