Skip to content

Bug: Lambda context access in batch processing record handler #1635

Closed
@erikayao93

Description

@erikayao93

Expected Behaviour

Based on the Python implementation, the record handler should be defined to accept a Context object in order to access the Lambda context.

Current Behaviour

Currently, the processor will pass the entire BatchProcessingOptions object to the handler, so if a handler is defined matching a Python handler, expecting a context parameter of type Context, the Context attributes and methods will be inaccessible.

Code snippet

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

Steps to Reproduce

This code is from the documentation example. When running the example, there will be an error that getRemainingTimeInMillis() is not a function.

Possible Solution

In order to have the code work by redefining the handler, it has to be something like this:

const recordHandler = (record: SQSRecord, options?: BatchProcessingOptions): void => {
    const payload = record.body;
    if (payload) {
        const item = JSON.parse(payload);
        logger.info('Processed item', { item });
    }
    if (options) {
        logger.info('Remaining time', {
            time: options.context.getRemainingTimeInMillis(),
        });
    }
};

Otherwise, the processor should be redefined to pass a Context object to the handler instead of a BatchProcessingOptions type object.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

16.x

Packaging format used

npm

Execution logs

"msg": "All records failed processing. 1 individual errors logged separately below.",
    "childErrors": [
        {
            "errorType": "TypeError",
            "errorMessage": "e.getRemainingTimeInMillis is not a function",
            "stack": [
                "TypeError: e.getRemainingTimeInMillis is not a function",
                "    at xe.Ys [as handler] (/var/task/index.js:7:24441)",
                "    at xe.processRecord (/var/task/index.js:4:3142)",
                "    at xe.process (/var/task/index.js:4:978)",
                "    at kr (/var/task/index.js:5:88)",
                "    at Runtime.$s [as handler] (/var/task/index.js:7:24514)",
                "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1083:29)"
            ]
        }
    ],

Metadata

Metadata

Assignees

Labels

batchThis item relates to the Batch Processing UtilitybugSomething isn't workingcompletedThis item is complete and has been merged/shipped

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions