generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 176
Open
Labels
batchThis item relates to the Batch Processing UtilityThis item relates to the Batch Processing Utilityfeature-requestThis item refers to a feature request for an existing or new utilityThis item refers to a feature request for an existing or new utility
Description
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
- This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
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 UtilityThis item relates to the Batch Processing Utilityfeature-requestThis item refers to a feature request for an existing or new utilityThis item refers to a feature request for an existing or new utility