Skip to content

Feature request: add support for durable function in Idempotency Utility #4831

@ConnorKirk

Description

@ConnorKirk

Expected Behavior

Idempotency Utility works correctly when a durable function is suspended and replayed

Current Behavior

  • Idempotency utility breaks in some Lambda durable executions
  • Will raise IdempotencyAlreadyInProgressError if a durable execution is suspended and then replayed, such as after a context.wait() call
  • Identified as a duplicate of an existing payload, rather than a replay of the original payload (which should be allowed)

Code snippet

import {
  withDurableExecution,
  DurableContext,
} from "aws-durable-execution-sdk-js";

import {
  IdempotencyConfig,
  makeIdempotent,
} from "@aws-lambda-powertools/idempotency";
import { DynamoDBPersistenceLayer } from "@aws-lambda-powertools/idempotency/dynamodb";

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName:
    process.env.IDEMPOTENCY_TABLE_NAME || "powertools-idempotency-table",
});

const innerHandler = async (event, context: DurableContext) => {
  config.registerLambdaContext(context.lambdaContext);

  try {
    console.log({ event });

    // This breaks the idempotency function as
    // the replay detects the existing "in progress" execution,
    // so throws an IdempotencyAlreadyInProgressError error.
    await context.wait("wait", 60);
    const processedData = await context.step(async () => {
      // Simulating idempotent operation
      return {
        id: "unique-id",
        timestamp: Date.now(),
      };
    });

    context.step(async () => {
      // Additional processing step
      console.log("Processing data:", processedData);
    });

    return {
      statusCode: 200,
      body: JSON.stringify({
        message: "Success",
        data: processedData,
      }),
    };
  } catch (error) {
    console.error(error);
    console.trace();
    return {
      statusCode: 500,
      body: JSON.stringify({
        message: "Error",
        error: error instanceof Error ? error.message : "Unknown error",
      }),
    };
  }
};

export const handler = withDurableExecution(
  makeIdempotent(innerHandler, {
    persistenceStore,
  })
);

Steps to Reproduce

  1. Create a lambda function with a durable config specified, and the handler above
  2. Invoke the function
  3. Observe an error is raised

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

24.x

Packaging format used

npm

Execution logs

Metadata

Metadata

Assignees

Labels

feature-requestThis item refers to a feature request for an existing or new utilityidempotencyThis item relates to the Idempotency Utilitypending-releaseThis item has been merged and will be released soon

Type

No type

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions