本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
叫用耐用的 Lambda 函數
持久性 Lambda 函數可以使用與預設 Lambda 函數相同的方法叫用,但對於長時間執行的執行有重要的考量。本節涵蓋叫用模式、執行管理和耐久函數的最佳實務。
同步調用限制
耐用 Lambda 函數的同步調用限制為 15 分鐘,與預設 Lambda 函數相同。如果您的耐用函數需要執行超過 15 分鐘,則必須以非同步方式叫用它。
何時使用同步調用:用於在 15 分鐘內完成的耐久函數,以及當您需要立即結果時,例如快速核准工作流程或短資料處理任務。
長時間執行工作流程的非同步調用
對於執行時間可能超過 15 分鐘的耐用函數,請使用非同步調用。這可讓函數在用戶端收到立即確認時繼續執行。
- TypeScript
-
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
const client = new LambdaClient({});
// Asynchronous invocation
const command = new InvokeCommand({
FunctionName: "my-durable-function",
InvocationType: "Event", // Asynchronous
Payload: JSON.stringify({ orderId: "12345" })
});
await client.send(command);
- Python
-
import boto3
import json
client = boto3.client('lambda')
# Asynchronous invocation
response = client.invoke(
FunctionName='my-durable-function',
InvocationType='Event', # Asynchronous
Payload=json.dumps({'order_id': '12345'})
)
執行管理 APIs
Lambda APIs 來管理和監控持久的函數執行,包括列出執行、取得執行狀態和停止執行。
- TypeScript
-
// Get execution status
const statusCommand = new InvokeCommand({
FunctionName: "my-durable-function",
InvocationType: "RequestResponse",
Payload: JSON.stringify({
action: "getStatus",
executionId: "exec-123"
})
});
const result = await client.send(statusCommand);
- Python
-
# Get execution status
response = client.invoke(
FunctionName='my-durable-function',
InvocationType='RequestResponse',
Payload=json.dumps({
'action': 'get_status',
'execution_id': 'exec-123'
})
)