

# 耐久性のある Lambda 関数の呼び出し
<a name="durable-invocation"></a>

耐久性のある Lambda 関数は、デフォルトの Lambda 関数と同じ方法を使用して呼び出すことができますが、長時間の実行には重要な考慮事項があります。このセクションでは、耐久性のある関数の呼び出しパターン、実行管理、ベストプラクティスについて説明します。

## 同期呼び出しの制限
<a name="synchronous-invocation-limits"></a>

耐久性のある Lambda 関数の同期呼び出しは、デフォルトの Lambda 関数と同様に 15 分に制限されます。耐久性のある関数を 15 分以上実行する必要がある場合、非同期的に呼び出す必要があります。

**同期呼び出しを使用するタイミング:** 15 分以内に完了する耐久性のある関数、ならびに結果 (迅速な承認ワークフローや短いデータ処理タスクなど) が即時に必要なときに使用します。

## 長時間のワークフローの非同期呼び出し
<a name="asynchronous-invocation"></a>

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'})
)
```

------

## 実行管理 API
<a name="execution-management-apis"></a>

Lambda では API が提供され、耐久性のある関数実行 (実行の一覧表示、実行ステータスの取得、実行の停止など) を管理およびモニタリングがされます。

------
#### [ 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'
    })
)
```

------