Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
erikayao93 opened this issue Jul 28, 2023 · 3 comments · Fixed by #1637
Closed

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

erikayao93 opened this issue Jul 28, 2023 · 3 comments · Fixed by #1637
Assignees
Labels
batch This item relates to the Batch Processing Utility bug Something isn't working completed This item is complete and has been merged/shipped

Comments

@erikayao93
Copy link
Contributor

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)"
            ]
        }
    ],
@erikayao93 erikayao93 added triage This item has not been triaged by a maintainer, please wait bug Something isn't working labels Jul 28, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation batch This item relates to the Batch Processing Utility and removed triage This item has not been triaged by a maintainer, please wait labels Jul 28, 2023
@dreamorosi
Copy link
Contributor

Hi Erika, thank you for opening this issue. From a first look in the codebase it seems that the issue is valid.

If you or anyone bumping into this is interested in proposing a PR, I'd be happy to help you get it merged. Otherwise we'll work on a fix before the next release later next week.

@dreamorosi dreamorosi added the help-wanted We would really appreciate some support from community for this one label Jul 28, 2023
@dreamorosi dreamorosi linked a pull request Jul 29, 2023 that will close this issue
9 tasks
@dreamorosi dreamorosi removed the help-wanted We would really appreciate some support from community for this one label Jul 29, 2023
@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Jul 29, 2023
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jul 29, 2023
@github-actions
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jul 29, 2023
@github-actions
Copy link
Contributor

This is now released under v1.13.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Sep 18, 2023
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
batch This item relates to the Batch Processing Utility bug Something isn't working completed This item is complete and has been merged/shipped
Projects
2 participants