Skip to content

Bug: idempotencyHandler not using registered context when saving in progress #1482

Closed
@dreamorosi

Description

@dreamorosi

Expected Behaviour

When using the idempotentLambdaHandler, idempotentFunction decorators, or makeFunctionIdempotent function wrapper the underlying idempotency handler should use the Lambda context to determine the remaining milliseconds in the current execution when saving a record as in-progress. This way, if an execution times out while processing it can be handled correctly later on.

Current Behaviour

The context is never used in the IdempotencyHandler module and the idempotentLambdaHandler never calls idempotencyConfig.registerLambdaContext(context) - those are two separate but related issues.

As a result, even though the operation is made idempotent, the handler logs this warning:

2023-06-02T16:42:39.239Z 7bac60e0-3084-403c-9c69-be9f20d0e761 WARN Could not determine remaining time left. Did you call registerLambdaContext on IdempotencyConfig?

Code snippet

import type { Context } from "aws-lambda";
import type { LambdaInterface } from "@aws-lambda-powertools/commons";
import { Logger } from "@aws-lambda-powertools/logger";
import { idempotentLambdaHandler } from "@aws-lambda-powertools/idempotency";
import { DynamoDBPersistenceLayer } from "@aws-lambda-powertools/idempotency/dynamodb";
import { IdempotencyConfig } from "@aws-lambda-powertools/idempotency";

type EventRecord = {
  foo: string;
};

const logger = new Logger();

const dynamoDBPersistenceLayer = new DynamoDBPersistenceLayer({
  tableName:
    process.env.IDEMPOTENCY_TABLE_NAME || "lambda-powertools-playground-table",
});
const idempotencyConfig = new IdempotencyConfig({});

class Lambda implements LambdaInterface {
  @idempotentLambdaHandler({
    persistenceStore: dynamoDBPersistenceLayer,
  })
  public async handler(event: EventRecord, _context: Context): Promise<string> {
    logger.info("Got test event:", { event });
    return "Processing done: " + event.foo;
  }
}

const defaultLambda = new Lambda();
export const handler = defaultLambda.handler.bind(defaultLambda);

Steps to Reproduce

  1. Deploy the code
  2. Run it
  3. Observe the warning in the logs & the absence of the in_progress_expiry_attr value in the DynamoDB Table

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

18.x

Packaging format used

npm

Execution logs

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedidempotencyThis item relates to the Idempotency Utility

Type

No type

Projects

Status

Shipped

Relationships

None yet

Development

No branches or pull requests

Issue actions