Skip to content

Feature request: Allow use of InvokeStore in Batch Processor #4676

@svozza

Description

@svozza

Use case

The InvokeStore pacakge, allow for the use of Asynchronous contexts in lambda handlers. We should allow users to process SQS, Kinesis and DynamoDB streams in async contexts across lambda executions so that they are isolated.

Solution/User Experience

Lambda handlers in the runtime are automatically wrapped in this context, this can be replicated locally like so:

import { BatchProcessor, EventType } from '@aws-lambda-powertools/batch';
import { Logger } from '@aws-lambda-powertools/logger';
import type { SQSHandler, SQSRecord } from 'aws-lambda';

const processor = new BatchProcessor(EventType.SQS);
const logger = new Logger();

const recordHandler = (record: SQSRecord): void => {
  const payload = record.body;
  if (payload) {
    const item = JSON.parse(payload);
    logger.info('Processed item', { item });
  }
};

export const handler: SQSHandler = async (event, context) => {
  const batch = event.Records; 
  
  // The records registered to the processor instance will be scoped only to this handler 
  // execution and will not be available in any other invocations
  processor.register(batch, recordHandler, { context }); 

  const processedMessages = await processor.process();

  for (const message of processedMessages) {
    const [status, error, record] = message;

    logger.info('Processed record', { status, record, error });
  }

  return processor.response();
};

// ... 

const result = InvokeStore.run(
  {
    [InvokeStore.PROTECTED_KEYS.REQUEST_ID]: "request-123",
    [InvokeStore.PROTECTED_KEYS.X_RAY_TRACE_ID]: "trace-456", // Optional X-Ray trace ID
  },
  () => {
    return handler(event)
  }
);

Alternative solutions

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

batchThis item relates to the Batch Processing Utilityfeature-requestThis item refers to a feature request for an existing or new utility

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions