durable-execution-sdk-js.withdurableexecution.md

December 2, 2025 ยท View on GitHub

Home > @aws/durable-execution-sdk-js > withDurableExecution

withDurableExecution() function

Wraps a durable handler function to create a handler with automatic state persistence, retry logic, and workflow orchestration capabilities.

This function transforms your durable handler into a function that integrates with the AWS Durable Execution service. The wrapped handler automatically manages execution state and checkpointing.

Signature:

withDurableExecution: <
  TEvent = any,
  TResult = any,
  TLogger extends DurableLogger = DurableLogger,
>(
  handler: DurableExecutionHandler<TEvent, TResult, TLogger>,
  config?: DurableExecutionConfig,
) => DurableLambdaHandler;

Parameters

Parameter

Type

Description

handler

DurableExecutionHandler<TEvent, TResult, TLogger>

Your durable handler function that uses the DurableContext for operations

config

DurableExecutionConfig

(Optional) Optional configuration for custom advanced settings

Returns:

DurableLambdaHandler

A handler function that automatically manages durability

Example 1

**Basic Usage:**

import {
  withDurableExecution,
  DurableExecutionHandler,
} from "@aws/durable-execution-sdk-js";

const durableHandler: DurableExecutionHandler<MyEvent, MyResult> = async (
  event,
  context,
) => {
  // Execute durable operations with automatic retry and checkpointing
  const userData = await context.step("fetch-user", async () =>
    fetchUserFromDB(event.userId),
  );

  // Wait for external approval
  const approval = await context.waitForCallback(
    "user-approval",
    async (callbackId) => {
      await sendApprovalEmail(callbackId, userData);
    },
  );

  // Process in parallel
  const results = await context.parallel("process-data", [
    async (ctx) => ctx.step("validate", () => validateData(userData)),
    async (ctx) => ctx.step("transform", () => transformData(userData)),
  ]);

  return { success: true, results };
};

export const handler = withDurableExecution(durableHandler);

Example 2

**With Custom Configuration:**

import { LambdaClient } from "@aws-sdk/client-lambda";

const customClient = new LambdaClient({
  region: "us-west-2",
  maxAttempts: 5,
});

export const handler = withDurableExecution(durableHandler, {
  client: customClient,
});

Example 3

**Passed Directly to the Handler:**

export const handler = withDurableExecution(async (event, context) => {
  const result = await context.step(async () => processEvent(event));
  return result;
});