使用 AWS Lambda Durable Functions 的简易请假管理

发布: (2026年1月14日 GMT+8 08:17)
6 min read
原文: Dev.to

I’m happy to translate the article for you, but I need the actual text you’d like translated. Could you please paste the content (or the portions you want translated) here? I’ll keep the source link, formatting, markdown, and any code blocks exactly as you specify.

介绍

AWS re:Invent 2025 上,Lambda 引入了 Durable Functions,并提供了一套出色的功能。

关键亮点

  • 单次执行可持续 最长一年
  • 内置检查点会跟踪已完成的步骤,因此在恢复或中断后重试执行时,已完成的步骤会被跳过,工作流从下一个步骤继续。

当涉及将多个 AWS 服务编排成工作流时,AWS Step Functions 长期以来一直是首选。使用 Durable Functions,您现在可以在熟悉的 Lambda 环境 内部 实现类似的编排,这非常棒!

在本文中,我将解释如何利用 Durable Functions 的回调功能 实现 人工参与 工作流。

场景概述

当执行开始时,Durable Functions 会启动一次 Lambda 调用。对于多步骤执行,您可以:

  1. 暂停(或等待)在特定步骤,等待一定的时间段 直到收到来自外部进程的信号。
  2. 在收到外部信号后,执行要么 恢复(成功时),要么 终止(失败时)。

此原生能力非常适合需要 人工批准 的业务流程。

示例:请假申请工作流

步骤描述
1员工发送请假请求 → Durable Function 开始一次执行。
2DynamoDB 表中创建一条状态为 pending 的请假记录。
3员工收到确认请求已收到的电子邮件。
4经理收到一封包含 回调 ID 的电子邮件,用于批准或拒绝请假。
5Durable Function 保持 执行,直至收到经理的决定。
6经理批准或拒绝 → 执行 恢复
7如果经理在超时期限内 操作,请求 过期
8在 DynamoDB 中更新请假状态。
9员工收到包含经理决定或过期通知的电子邮件。

Architecture Details

1. Proxy Function

  • Purpose: Accept and validate the incoming leave‑request payload.
  • Implementation: Exposed via a Lambda Function URL.
  • Why needed?
    • Durable Functions’ synchronous invocations are limited to 15 minutes.
    • To run longer, we invoke the durable function asynchronously (InvocationType='Event'), i.e., fire‑and‑forget.

2. Durable Function Execution Flow

  1. Create leave record in DynamoDB.

  2. Send email to the employee confirming receipt.

  3. Send email to the manager with a callback ID and a link to approve/reject.

  4. Wait for callback (wait_for_callback step). Execution pauses here.

  5. Callback handling – a separate Lambda (exposed via Function URL) receives callback_id and decision.

    {
        "callback_id": "abc123-def456-ghi789",
        "decision": "approve"
    }
  6. The callback Lambda calls the new SDK methods:

    # Pseudo‑code
    lambda_client.send_durable_execution_callback_success(
        CallbackId=callback_id,
        Output={"status": "APPROVED"}
    )
    # or
    lambda_client.send_durable_execution_callback_failure(
        CallbackId=callback_id,
        ErrorMessage="Rejected by manager"
    )

    Note: These methods are not available in the default boto3 version shipped with Lambda. Package boto3 ≥ 1.42.1 with your function code.

  7. Upon receiving the callback, the durable execution restarts.

  8. If the manager does not respond within the timeout (e.g., 5 minutes), wait_for_callback raises a CallableRuntimeError with the message “Callback timed out.”

  9. The leave record is updated accordingly (approved, rejected, or expired).

  10. A final email is sent to the employee, and the execution completes.

亲自尝试

一个完整的示例项目(AWS CDK + Python)已在 GitHub 上提供:

🔗 仓库: https://github.com/pubudusj/simple-leave-management-with-durable-functions

设置步骤

  1. 克隆仓库

    git clone https://github.com/pubudusj/simple-leave-management-with-durable-functions.git
    cd simple-leave-management-with-durable-functions
  2. 配置环境变量

    cp .env.example .env
    # 编辑 .env 并填写所需的值。
    # 确保 SYSTEM_FROM_EMAIL 已在 Amazon SES 中通过验证。
  3. 部署堆栈

    cdk deploy

    部署完成后,输出中会显示 两个 Lambda Function URL

    • 创建请假请求 端点
    • 处理请假请求(经理)端点

1. 创建请假请求

curl -X POST \
     -H "Content-Type: application/json" \
     -d '{
           "start_date":"2026-01-10",
           "end_date":"2026-01-20",
           "employee_email":"employee@email.com"
         }' \
     <CREATE_LEAVE_REQUEST_URL>
  • 结果
    • 员工会收到确认邮件。
    • 经理会收到一封包含 回调 ID 和批准或拒绝请求链接的邮件。

2. 经理批准 / 拒绝

curl -X POST \
     -H "Content-Type: application/json" \
     -d '{
           "callback_id":"<CALLBACK_ID>",
           "decision":"approve"
         }' \
     <PROCESS_LEAVE_REQUEST_URL>
  • 结果
    • 持久化执行恢复,更新 DynamoDB 记录,并向员工发送最终状态邮件。

3. 超时情形

如果经理在配置的超时时间内(演示默认 5 分钟采取行动,请假请求将自动标记为 已过期,并向员工发送过期通知。

摘要

  • Durable Functions 现在支持长时间运行、带检查点的工作流(最长一年)。
  • callback 模式使得 human‑in‑the‑loop 步骤无需外部状态机即可实现。
  • 使用 proxy Lambda 可以进行异步调用,绕过 15 分钟的同步限制。
  • 示例项目展示了一个完整的端到端请假流程,使用 DynamoDBSESLambda callbacks

试一试吧,并告诉我你如何将此模式扩展到自己的业务流程中!

OWS

请尝试一下并告诉我你的想法!

Back to Blog

相关文章

阅读更多 »