AWS Lambdaの耐久的機能(長時間実行に耐える仕組み)を使い、信頼性の高い複数段階のアプリケーションを構築します。自動的に処理状態を保存し、失敗時の再試行ロジック、そして処理全体の統制機能(オーケストレーション)を備えています。 次のような内容をカバー: - リプレイモデル(過去の処理を再実行する仕組み)の重要な役割 - 各ステップの操作方法 - 待機やコールバック(完了を知らせる通知)のパターン - エラー処理のためのサガパターン(複数の処理を安全につなぐ方法) - LocalDurableTestRunnerを使ったテスト 次のような場合に使用: Lambda耐久的機能、ワークフロー統制、状態機械(処理の流れを状態で管理する仕組み)、再試行・チェックポイント(進捗地点を保存する)パターン、長時間実行する段階的なLambda処理、サガパターン、人による判断を含むコールバック、信頼性の高いサーバーレスアプリケーション
Build resilient, long-running, multi-step applications with AWS Lambda durable functions with automatic state persistence, retry logic, and orchestration for long-running executions. Covers the critical replay model, step operations, wait/callback patterns, error handling with saga pattern, testing with LocalDurableTestRunner. Triggers on phrases like: lambda durable functions, workflow orchestration, state machines, retry/checkpoint patterns, long-running stateful Lambda functions, saga pattern, human-in-the-loop callbacks, and reliable serverless applications.
中断が発生しても確実に進行状態を維持しながら、最大1年間実行可能な、 耐障害性の高いマルチステップアプリケーションおよびAIワークフローを構築します。
AWS Lambda 耐久性関数を使用する前に、以下を確認してください:
AWS CLI がインストール済み(2.33.22 以上)かつ設定済みであること:
aws --version
aws sts get-caller-identity
ランタイム環境 が整っていること:
node --version)python --version)
注意: 現時点では、Durable Execution SDK がプリインストールされているのは Lambda ランタイム環境 3.13以上のみです。 Durable SDK 自体がサポートする最低 Python バージョンは 3.11 ですが、 OCI を使用して独自の Python ランタイムと Durable SDK を含むコンテナイメージを持ち込むことも可能です。
デプロイ手段 が存在すること(いずれか一つ):
sam --version)1.153.1 以上cdk --version)v2.237.1 以上デフォルト: TypeScript
上書き構文:
"use Python" → Python コードを生成"use JavaScript" → JavaScript コードを生成指定がない場合は、常に TypeScript を使用してください。
デフォルト: CDK
上書き構文:
"use CloudFormation" → YAML テンプレートを生成"use SAM" → YAML テンプレートを生成指定がない場合は、常に CDK を使用してください。
TypeScript/JavaScript の場合:
npm install @aws/durable-execution-sdk-js
npm install --save-dev @aws/durable-execution-sdk-js-testing
Python の場合:
pip install aws-durable-execution-sdk-python
pip install aws-durable-execution-sdk-python-testing
ユーザーが取り組んでいる内容に応じて、適切なリファレンスファイルを参照してください:
入門・基本セットアップ・サンプル・ESLint・Jest セットアップ → getting-started.md を参照
リプレイモデルの理解・決定論・非決定論的エラー → replay-model-rules.md を参照
ステップの作成・アトミック操作・リトライロジック → step-operations.md を参照
待機・遅延・コールバック・外部システム・ポーリング → wait-operations.md を参照
並列実行・マップ操作・バッチ処理・並行性 → concurrent-operations.md を参照
エラーハンドリング・リトライ戦略・Saga パターン・補償トランザクション → error-handling.md を参照
高度なエラーハンドリング・タイムアウト処理・サーキットブレーカー・条件付きリトライ → advanced-error-handling.md を参照
テスト・ローカルテスト・クラウドテスト・テストランナー・フレイキーテスト → testing-patterns.md を参照
デプロイ・CloudFormation・CDK・SAM・ロググループ・インフラストラクチャ → deployment-iac.md を参照
高度なパターン・GenAI エージェント・完了ポリシー・ステップセマンティクス・カスタムシリアライゼーション → advanced-patterns.md を参照
トラブルシューティング・実行停止・実行失敗・実行 ID のデバッグ・実行履歴・実行エラー・実行失敗の原因・実行タイムアウト・コールバック未受信・実行診断・根本原因分析 → troubleshooting-executions.md を参照
TypeScript:
import { withDurableExecution, DurableContext } from '@aws/durable-execution-sdk-js';
export const handler = withDurableExecution(async (event, context: DurableContext) => {
const result = await context.step('process', async () => processData(event));
return result;
});
Python:
from aws_durable_execution_sdk_python import durable_execution, DurableContext
@durable_execution
def handler(event: dict, context: DurableContext) -> dict:
result = context.step(lambda _: process_data(event), name='process')
return result
Date.now、Math.random、API 呼び出し、など)runInChildContext を使用することcontext.logger(リプレイ対応)を使用することPython SDK は TypeScript と以下の主要な点で異なります:
@durable_step デコレーターと context.step(my_step(args)) を組み合わせて使用するか、
インラインで context.step(lambda _: ..., name='...') を使用します。
ステップ名の自動付与のため、デコレーターの使用を推奨します。context.wait(duration=Duration.from_seconds(n), name='...')ExecutionError(永続的エラー)、InvocationError(一時的エラー)、CallbackError(コールバック失敗)DurableFunctionTestRunner クラスを直接使用します。
ハンドラーでインスタンス化し、コンテキストマネージャーを使用して run(input=...) を呼び出します。耐久性関数の呼び出しには 修飾 ARN(バージョン、エイリアス、または $LATEST)が必要です:
# 有効
aws lambda invoke --function-name my-function:1 output.json
aws lambda invoke --function-name my-function:prod output.json
# 無効 — 失敗します
aws lambda invoke --function-name my-function output.json
Lambda 実行ロールには、マネージドポリシー AWSLambdaBasicDurableExecutionRolePolicy が
アタッチされている必要があります。このポリシーには以下が含まれます:
lambda:CheckpointDurableExecution — 実行状態の永続化lambda:GetDurableExecutionState — 実行状態の取得以下の操作には追加権限が必要です:
lambda:InvokeFunctionlambda:SendDurableExecutionCallbackSuccess および
lambda:SendDurableExecutionCallbackFailure が必要耐久性関数のコードを記述・レビューする際は、以下のリプレイモデル違反を必ず確認してください:
ステップ外の非決定論的コード:
Date.now()、Math.random()、UUID 生成、API 呼び出し、データベースクエリは
すべてステップ内に記述すること
ステップ関数内での耐久性操作のネスト:
ステップ関数の内部で context.step()、context.wait()、context.invoke() を
呼び出すことは不可 — 代わりに context.runInChildContext() を使用すること
リプレイをまたいで保持されないクロージャへの変更: ステップ内部でミュートされた変数はリプレイをまたいで保持されません — ステップからは値を返すようにすること
リプレイ時に繰り返されるステップ外のサイドエフェクト:
ロギングには context.logger を使用すること
(リプレイ対応で自動的に重複を排除します)
耐久性関数のテストを実装・修正する際は、以下を必ず確認してください:
LocalDurableTestRunner を使用すること書き込みアクセスはデフォルトで有効です。
このプラグインは .mcp.json に --allow-write が設定された状態で提供されるため、
MCP Server はユーザーに代わってプロジェクトの作成、IaC の生成、デプロイを行うことができます。
Lambda および API Gateway のログなどの機密データへのアクセスはデフォルトでは無効です。
有効にするには、.mcp.json に --allow-sensitive-data-access を追加してください。
Build resilient multi-step applications and AI workflows that can execute for up to 1 year while maintaining reliable progress despite interruptions.
Before using AWS Lambda durable functions, verify:
AWS CLI is installed (2.33.22 or higher) and configured:
aws --version
aws sts get-caller-identity
Runtime environment is ready:
node --version)python --version. Note that currently only Lambda runtime environments 3.13+ come with the Durable Execution SDK pre-installed. 3.11 is the min supported Python version by the Durable SDK itself, however, you could use OCI to bring your own container image with your own Python runtime + Durable SDK.)Deployment capability exists (one of):
sam --version) 1.153.1 or highercdk --version) v2.237.1 or higherDefault: TypeScript
Override syntax:
When not specified, ALWAYS use TypeScript
Default: CDK
Override syntax:
When not specified, ALWAYS use CDK
For TypeScript/JavaScript:
npm install @aws/durable-execution-sdk-js
npm install --save-dev @aws/durable-execution-sdk-js-testing
For Python:
pip install aws-durable-execution-sdk-python
pip install aws-durable-execution-sdk-python-testing
Load the appropriate reference file based on what the user is working on:
TypeScript:
import { withDurableExecution, DurableContext } from '@aws/durable-execution-sdk-js';
export const handler = withDurableExecution(async (event, context: DurableContext) => {
const result = await context.step('process', async () => processData(event));
return result;
});
Python:
from aws_durable_execution_sdk_python import durable_execution, DurableContext
@durable_execution
def handler(event: dict, context: DurableContext) -> dict:
result = context.step(lambda _: process_data(event), name='process')
return result
runInChildContext to group operationscontext.logger (replay-aware)The Python SDK differs from TypeScript in several key areas:
@durable_step decorator + context.step(my_step(args)), or inline context.step(lambda _: ..., name='...'). Prefer the decorator for automatic step naming.context.wait(duration=Duration.from_seconds(n), name='...')ExecutionError (permanent), InvocationError (transient), CallbackError (callback failures)DurableFunctionTestRunner class directly - instantiate with handler, use context manager, call run(input=...)Durable functions require qualified ARNs (version, alias, or $LATEST):
# Valid
aws lambda invoke --function-name my-function:1 output.json
aws lambda invoke --function-name my-function:prod output.json
# Invalid - will fail
aws lambda invoke --function-name my-function output.json
Your Lambda execution role MUST have the AWSLambdaBasicDurableExecutionRolePolicy managed policy attached. This includes:
lambda:CheckpointDurableExecution - Persist execution statelambda:GetDurableExecutionState - Retrieve execution stateAdditional permissions needed for:
lambda:InvokeFunction on target function ARNslambda:SendDurableExecutionCallbackSuccess and lambda:SendDurableExecutionCallbackFailureWhen writing or reviewing durable function code, ALWAYS check for these replay model violations:
Date.now(), Math.random(), UUID generation, API calls, database queries must all be inside stepscontext.step(), context.wait(), or context.invoke() inside a step function — use context.runInChildContext() insteadcontext.logger for logging (it is replay-aware and deduplicates automatically)When implementing or modifying tests for durable functions, ALWAYS verify:
LocalDurableTestRunner for local testingWrite access is enabled by default. The plugin ships with --allow-write in .mcp.json, so the MCP server can create projects, generate IaC, and deploy on behalf of the user.
Access to sensitive data (like Lambda and API Gateway logs) is not enabled by default. To grant it, add --allow-sensitive-data-access to .mcp.json.
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。