本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Lambda 耐久函數建立訂單處理系統
注意
NEED:新增顯示 API Gateway、耐用函數工作流程和支援服務的架構圖 (DynamoDB、EventBridge)
先決條件
AWS CLI 已安裝和設定
需求:特定耐用函數需求
建立來源碼檔案
在專案目錄中建立下列檔案:
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 資料表
請在 https://console.aws.amazon.com/dynamodb/
開啟 DynamoDB 主控台。 選擇建立資料表
針對資料表名稱,輸入
Orders針對分割區索引鍵,輸入
orderId將其他設定保留為預設值
選擇建立資料表
建立 Lambda 函數
在 https://https://console.aws.amazon.com/lambda/
開啟 Lambda 主控台 選擇 Create function (建立函數)
從頭選取作者
在函數名稱輸入
ProcessOrder針對執行期,選擇您偏好的執行期
NEED:新增耐用函數特定的組態
選擇 Create function (建立函數)
建立 API Gateway 端點
在 https://https://console.aws.amazon.com/apigateway/
開啟 API Gateway 主控台 選擇建立 API
選取 HTTP API
選擇建置
新增與您的 Lambda 函數的整合
設定訂單處理的路由
部署 API
測試應用程式
提交測試順序:
{ "orderId": "12345", "items": [ { "productId": "ABC123", "quantity": 1 } ] }
NEED:新增持久性函數的特定監控指示
後續步驟
新增商業邏輯
實作庫存管理:
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 儀表板
設定訂單處理時間的指標
設定延遲訂單的提醒