Lambda 耐久関数を使用した注文処理システムの作成
注記
NEED: API Gateway、Durable Function ワークフロー、サポートサービス (DynamoDB、EventBridge) を示すアーキテクチャ図を追加する
前提条件
AWS CLIインストールおよび設定済みの
NEED: 特定の Durable Functions の要件
ソースコードファイルを作成する
プロジェクトディレクトリに次のファイルを作成します。
lambda_function.py– 関数コードrequirements.txt– 依存関係マニフェスト
関数コード
# NEED: Verify correct imports import boto3 import json def lambda_handler(event, context): # NEED: Verify DurableContext syntax durable = context.durable try: # Validate and store order order = await durable.step('validate', async () => { return validate_order(event['order']) }) # Process payment # NEED: Verify wait syntax await durable.wait(/* wait configuration */) # Additional steps # NEED: Complete implementation except Exception as e: # NEED: Error handling patterns raise e def validate_order(order_data): # NEED: Implementation pass
要件ファイル
# NEED: List of required packages
アプリケーションのデプロイ
注文用の DynamoDB テーブルを作成する
DynamoDB コンソール (https://console.aws.amazon.com/dynamodb/
) を開きます。 [Create table (テーブルの作成)] を選択します。
[テーブル名] に「
Orders」と入力します。[パーティションキー] に「
orderId」と入力します。他の設定はすべてデフォルトのままにする
[Create table (テーブルの作成)] を選択します。
Lambda 関数を作成する
Lambda コンソール (https://console.aws.amazon.com/lambda/
) を開く [Create function] (関数の作成) を選択します。
[一から作成] を選択します。
[関数名] に
ProcessOrderと入力します。[ランタイム] では、任意のランタイムを選択します。
NEED: Durable Functions 固有の設定を追加する
[Create function] (関数の作成) を選択します。
API ゲートウェイエンドポイントを作成する
API ゲートウェイコンソール (https://console.aws.amazon.com/apigateway/
) を開く [API を作成] を選択する
[HTTP API] を選択する
[ビルド] を選択する
Lambda 関数との統合を追加する
注文処理のルートを設定する
API をデプロイする
アプリのテスト
テストオーダーを送信する:
{ "orderId": "12345", "items": [ { "productId": "ABC123", "quantity": 1 } ] }
NEED: Durable Functions に特定のモニタリング手順を追加する
次のステップ
ビジネスロジックを追加する
インベントリ管理を実装する:
async def check_inventory(order): # Add inventory check logic pass
料金計算を追加する:
async def calculate_total(order): # Add pricing logic pass
エラー処理を改善する
補償ロジックを追加する:
async def reverse_payment(order): # Add payment reversal logic pass
注文のキャンセルを処理する:
async def cancel_order(order): # Add cancellation logic pass
外部システムを統合する
async def notify_shipping_provider(order): # Add shipping integration pass async def send_customer_notification(order): # Add notification logic pass
モニタリングを拡張する
CloudWatch ダッシュボードを作成する
注文処理時間のメトリクスを設定する
遅延注文のアラートを設定する