Amazon Bedrock AgentCore ライフサイクル設定を構成する
CreateAgentRuntime LifecycleConfigurationへの入力パラメータを使用すると、Amazon Bedrock AgentCore Runtime のランタイムセッションとリソースのライフサイクルを管理できます。この設定は、アイドルセッションを自動的にクリーンアップし、長時間実行されるインスタンスがリソースを無期限に消費しないようにすることで、リソース使用率を最適化するのに役立ちます。
UpdateAgentRuntime オペレーションを使用して、既存の AgentCore ランタイムのライフサイクル設定を構成することもできます。
トピック
設定属性
| 属性 | タイプ | 範囲 (秒) | 必要 | 説明 |
|---|---|---|---|---|
|
|
整数 |
60-28800 |
いいえ |
アイドルランタイムセッションの秒単位のタイムアウト。この間セッションがアイドル状態のままになると、終了がトリガーされます。ログ記録やその他のプロセスの完了により、終了まで最大 15 秒かかる場合があります。デフォルト: 900 秒 (15 分) |
|
|
整数 |
60-28800 |
いいえ |
秒単位のインスタンスの最大有効期間。に達すると、インスタンスは終了を初期化します。ログ記録やその他のプロセスの完了により、終了まで最大 15 秒かかる場合があります。デフォルト: 28800 秒 (8 時間)。セッション自体は、プロビジョニングされた新しいインスタンスでこれを超えて保持できます。 |
制約
-
idleRuntimeSessionTimeoutは以下である必要がありますmaxLifetime -
両方の値は秒単位で測定されます
-
有効範囲: 60~28800 秒 (最大 8 時間)
デフォルトの 動作
が指定されLifecycleConfigurationていない場合、または null 値が含まれている場合、プラットフォームは次のロジックを適用します。
| 顧客入力 | idleRuntimeSessionTimeout | maxLifetime | 結果 |
|---|---|---|---|
|
設定なし |
900 秒 |
28800 秒 |
デフォルトを使用: 900 秒と 28800 秒 |
|
maxLifetime のみ提供 |
900 秒 |
顧客値 |
maxLifetime ≤ 900s の場合: は両方とも maxLifetime を使用します。maxLifetime > 900s の場合: はアイドル時に 900s を使用し、最大には顧客値を使用します。 |
|
idleTimeout のみ提供 |
顧客値 |
28800 秒 |
アイドルの場合は顧客値を使用し、最大の場合は 28800 秒 |
|
指定された両方の値 |
顧客値 |
顧客値 |
顧客値をそのまま使用します |
デフォルト値
-
idleRuntimeSessionTimeout: 900 秒 (15 分) -
maxLifetime: 28800 秒 (8 時間)
ライフサイクル設定を使用して AgentCore ランタイムを作成する
AgentCore ランタイムを作成するときにライフサイクル設定を指定できます。
import boto3 client = boto3.client('bedrock-agentcore-control', region_name='us-west-2') try: response = client.create_agent_runtime( agentRuntimeName='my_agent_runtime', agentRuntimeArtifact={ 'containerConfiguration': { 'containerUri': '123456789012.dkr.ecr.us-west-2.amazonaws.com/my-agent:latest' } }, lifecycleConfiguration={ 'idleRuntimeSessionTimeout': 1800, # 30 minutes, configurable 'maxLifetime': 14400 # 4 hours }, networkConfiguration={'networkMode': 'PUBLIC'}, roleArn='arn:aws:iam::123456789012:role/AgentRuntimeRole' ) print(f"Agent runtime created: {response['agentRuntimeArn']}") except client.exceptions.ValidationException as e: print(f"Validation error: {e}") except Exception as e: print(f"Error creating agent runtime: {e}")
AgentCore ランタイムのライフサイクル設定を更新する
既存の AgentCore ランタイムのライフサイクル設定を更新できます。
import boto3 client = boto3.client('bedrock-agentcore-control', region_name='us-west-2') agent_runtime_id = 'my_agent_runtime' try: response = client.update_agent_runtime( agentRuntimeId=agent_runtime_id, agentRuntimeArtifact={ 'containerConfiguration': { 'containerUri': '123456789012.dkr.ecr.us-west-2.amazonaws.com/my-agent:latest' } }, networkConfiguration={'networkMode': 'PUBLIC'}, roleArn='arn:aws:iam::123456789012:role/AgentRuntimeRole', lifecycleConfiguration={ 'idleRuntimeSessionTimeout': 600, # 10 minutes 'maxLifetime': 7200 # 2 hours } ) print("Lifecycle configuration updated successfully") except client.exceptions.ValidationException as e: print(f"Validation error: {e}") except client.exceptions.ResourceNotFoundException: print("Agent runtime not found") except Exception as e: print(f"Error updating configuration: {e}")
AgentCore ランタイムのライフサイクル設定を取得する
既存の AgentCore ランタイムのライフサイクル設定を取得できます。
import boto3 client = boto3.client('bedrock-agentcore-control', region_name='us-west-2') def get_lifecycle_config(): try: response = client.get_agent_runtime(agentRuntimeId="my_agent_runtime") lifecycle_config = response.get('lifecycleConfiguration', {}) idle_timeout = lifecycle_config.get('idleRuntimeSessionTimeout', 900) max_lifetime = lifecycle_config.get('maxLifetime', 28800) print(f"Current configuration:") print(f" Idle timeout: {idle_timeout}s ({idle_timeout//60} minutes)") print(f" Max lifetime: {max_lifetime}s ({max_lifetime//3600} hours)") return lifecycle_config except Exception as e: print(f"Error retrieving configuration: {e}") return None # Usage config = get_lifecycle_config() print(config)
検証と制約
ライフサイクル設定には、無効な設定を防ぐための検証ルールと制約が含まれています。
一般的な検証エラー
import boto3 client = boto3.client('bedrock-agentcore-control', region_name='us-west-2') try: client.create_agent_runtime( agentRuntimeName='invalid_config_agent', agentRuntimeArtifact={ 'containerConfiguration': { 'containerUri': '123456789012.dkr.ecr.us-west-2.amazonaws.com/my-agent:latest' } }, lifecycleConfiguration={ 'idleRuntimeSessionTimeout': 3600, # 1 hour, configurable 'maxLifetime': 1800 # 30 minutes - INVALID! }, networkConfiguration={'networkMode': 'PUBLIC'}, roleArn='arn:aws:iam::123456789012:role/AgentRuntimeRole', ) except client.exceptions.ValidationException as e: print(f"Validation failed: {e}") # Output: idleRuntimeSessionTimeout must be less than or equal to maxLifetime
検証ヘルパー関数
def validate_lifecycle_config(idle_timeout, max_lifetime): """Validate lifecycle configuration before API call""" errors = [] # Check range constraints if not (1 <= idle_timeout <= 28800): errors.append(f"idleRuntimeSessionTimeout must be between 1 and 28800 seconds") if not (1 <= max_lifetime <= 28800): errors.append(f"maxLifetime must be between 1 and 28800 seconds") # Check relationship constraint if idle_timeout > max_lifetime: errors.append(f"idleRuntimeSessionTimeout ({idle_timeout}s) must be ≤ maxLifetime ({max_lifetime}s)") return errors # Usage errors = validate_lifecycle_config(3600, 1800) if errors: for error in errors: print(f"Validation error: {error}") else: print("Configuration is valid")
ライフサイクル設定とランタイムセッション
定義したライフサイクル設定は、個々のランタイムセッションに適用されます。特定の を使用してエージェントを呼び出すとruntimeSessionId、AgentCore ランタイムはそのセッション専用のmicroVM をプロビジョニングします。ライフサイクルタイムアウト ( idleRuntimeSessionTimeoutおよび maxLifetime ) は、その特定の microVM インスタンスのライフサイクルを管理します。
import boto3 import json import uuid client = boto3.client('bedrock-agentcore', region_name='us-west-2') # Each unique runtimeSessionId gets its own microVM with lifecycle settings applied session_id_user_1 = str(uuid.uuid4()) # User 1's session session_id_user_2 = str(uuid.uuid4()) # User 2's session # First invocation for User 1 - creates new microVM with lifecycle timers response1 = client.invoke_agent_runtime( agentRuntimeArn='arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/my-agent', runtimeSessionId=session_id_user_1, # Dedicated microVM for this session payload=json.dumps({"prompt": "Hello from User 1"}).encode() ) # First invocation for User 2 - creates separate microVM with its own lifecycle timers response2 = client.invoke_agent_runtime( agentRuntimeArn='arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/my-agent', runtimeSessionId=session_id_user_2, # Different microVM for this session payload=json.dumps({"prompt": "Hello from User 2"}).encode() ) # Subsequent invocations to same session reuse the existing microVM # The idle timeout resets with each invocation to the same session response3 = client.invoke_agent_runtime( agentRuntimeArn='arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/my-agent', runtimeSessionId=session_id_user_1, # Reuses User 1's existing microVM payload=json.dumps({"prompt": "Follow-up from User 1"}).encode() )
ライフサイクル設定とセッションに関する重要なポイント:
-
セッションごとの分離: 各 は独立したライフサイクルタイマーを持つ独自のmicroVM
runtimeSessionIdを取得します。 -
アイドルタイマーのリセット: 同じセッションを呼び出すたびに が
idleRuntimeSessionTimeoutリセットされます。 -
最大有効期間の適用:
maxLifetimeタイマーはmicroVM が最初に作成されたときに開始され、リセットできません -
セッション終了 : いずれかのタイムアウトに達すると、その特定のセッションのmicroVM のみが終了します。セッションは、プロビジョニングされた新しい microVM で再開できます。
ヒント
各 microVM セッションは、microVM agentRuntimeArtifact の作成時にデプロイされたコードアセット () を使用します。エージェントのランタイムを新しいコードで更新した場合、既存のセッションは、終了して新しいセッションが作成されるまで、以前のバージョンを使用し続けます。
ベストプラクティス
リソース使用率とユーザーエクスペリエンスを最適化するためにライフサイクル設定を構成するときは、以下のベストプラクティスに従ってください。
推奨事項
-
デフォルト (900 秒アイドル、最大 28800 秒) で開始し、使用パターンに基づいて調整する
-
セッション期間をモニタリングしてタイムアウト値を最適化する
-
開発環境のタイムアウトを短くしてコストを削減する
-
ユーザーエクスペリエンスを検討する - タイムアウトが短すぎるとアクティブなユーザーが中断される可能性があります
-
非本番環境で最初に設定変更をテストする
-
特定のユースケースのタイムアウトの根拠を文書化する
一般的なパターン
| ユースケース | アイドルタイムアウト | 最大有効期間 | 根拠 |
|---|---|---|---|
|
インタラクティブチャット |
10~15 分 |
2~4 時間 |
応答性とリソース使用量のバランスをとる |
|
バッチ処理 |
30 分 |
8 時間 |
長時間実行されるオペレーションを許可する |
|
開発 |
5 分 |
30 分 |
コスト最適化のための迅速なクリーンアップ |
|
本番稼働用 API |
15 分 |
4 時間 |
標準の本番ワークロード |
|
デモ/テスト |
2 分 |
15 分 |
一時的な使用のための積極的なクリーンアップ |