Skip to content

Docs: Correction of code sample using idempotent_function in combination with Batch utility #1745

Closed

Description

@kmkhr

What were you searching in the docs?

I was looking into ways to combine Idempotency utility and Batch utility.

Is this related to an existing documentation section?

https://awslabs.github.io/aws-lambda-powertools-python/2.3.1/utilities/idempotency/#idempotent_function-decorator

How can we improve?

The code sample using batch_processor decorator has the following calling order, where save_inprogress is called before register_lambda_context is called.

batch_processor -> idempotent_function -> save_inprogress -> record_handler -> idempotent_function -> save_inprogress -> dummy -> register_lambda_context

Got a suggestion in mind?

Shouldn't we change to using context manager instead of decorator?
Also, shouldn't the dummy call be after register_lambda_context?

def handler(event, context):
    config.register_lambda_context(context) # see Lambda timeouts section
    # `data` parameter must be called as a keyword argument to work
    dummy("hello", "universe", data="test")
    batch = event["Records"]
    with processor(records=batch, handler=record_handler):
        processor.process()
    return processor.response()

Acknowledgment

  • I understand the final update might be different from my proposed suggestion, or refused.

Activity

  1. added
    documentation
    triage
    on Nov 23, 2022
  2. self-assigned this
    on Nov 24, 2022
  3. heitorlessa commented on Nov 24, 2022

    @heitorlessa
    Contributor

    hey @kt-hr great catch on register_lambda_context order - fixing that now.

    As to your question on context manager, there is no need here unless you want to inspect the response or handle when the entire batch fails differently - see here.

  4. removed
    triage
    on Nov 24, 2022
  5. added
    pending-release
    on Nov 24, 2022
  6. github-actions commented on Nov 24, 2022

    @github-actions
    Contributor

    This is now released under 2.4.0 version!

  7. removed
    pending-release
    on Nov 24, 2022
  8. kmkhr commented on Nov 24, 2022

    @kmkhr
    ContributorAuthor

    hi @heitorlessa thanks for the quick correction.

    I understood the difference between context manager and decorator in the batch utility. However, @idempotent_function is processed before register_lambda_context in decorator, which conflicts with the following sentence in the documentation.

    If you are only using the @idempotent_function decorator to guard isolated parts of your code, you must use register_lambda_context available in the idempotency config object to benefit from this protection.

    Furthermore, when the code sample is run, lambda outputs the following warnings on every cold start.

    Couldn't determine the remaining time left. 
    Did you call register_lambda_context on IdempotencyConfig?
    

    To suppress this warning, I have purposely described a code sample using context manger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentation

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions