Lambda 耐久関数を使用した注文処理システムの作成 - AWS Lambda

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 テーブルを作成する

  1. DynamoDB コンソール (https://console.aws.amazon.com/dynamodb/) を開きます。

  2. [Create table (テーブルの作成)] を選択します。

  3. [テーブル名] に「Orders」と入力します。

  4. [パーティションキー] に「orderId」と入力します。

  5. 他の設定はすべてデフォルトのままにする

  6. [Create table (テーブルの作成)] を選択します。

Lambda 関数を作成する

  1. Lambda コンソール (https://console.aws.amazon.com/lambda/) を開く

  2. [Create function] (関数の作成) を選択します。

  3. [一から作成] を選択します。

  4. [関数名] に ProcessOrder と入力します。

  5. [ランタイム] では、任意のランタイムを選択します。

  6. NEED: Durable Functions 固有の設定を追加する

  7. [Create function] (関数の作成) を選択します。

API ゲートウェイエンドポイントを作成する

  1. API ゲートウェイコンソール (https://console.aws.amazon.com/apigateway/) を開く

  2. [API を作成] を選択する

  3. [HTTP API] を選択する

  4. [ビルド] を選択する

  5. Lambda 関数との統合を追加する

  6. 注文処理のルートを設定する

  7. 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 ダッシュボードを作成する

  • 注文処理時間のメトリクスを設定する

  • 遅延注文のアラートを設定する