View a markdown version of this page

実行ロールでのターミナルコマンドの使用 - Amazon Bedrock AgentCore

実行ロールでのターミナルコマンドの使用

Amazon S3 からファイルをアップロード/ダウンロードする実行ロールを使用して、カスタム Code Interpreter ツールを作成できます。これにより、コードは S3 バケットとやり取りしてデータを保存および取得できます。

前提条件

S3 アクセスでカスタムコードインタープリタを作成する前に、以下を行う必要があります。

  1. S3 バケットを作成する (例: DOC-EXAMPLE-BUCKET )

  2. バケット内にフォルダを作成する (例: output_artifacts )

  3. 次の信頼ポリシーを使用して IAM ロールを作成します。

    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock-agentcore.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "StringEquals": { "aws:SourceAccount": "111122223333" } } } ] }
  4. ロールに次のアクセス許可を追加します。

    { "Version":"2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject" ], "Resource": "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*", "Condition": { "StringEquals": { "s3:ResourceAccount": "${aws:PrincipalAccount}" } } } ] }

Python コードサンプル

boto3 (SDK for Python) を使用して S3 統合を実装できます。AWS 次の例では、boto3 を使用して、Amazon S3 にファイルをアップロードしたり、Amazon S3 からファイルをダウンロードしたりできる実行ロールを持つカスタム Code Interpreter を作成します。

注記

このコードを実行する前に、 REGIONと を AWS リージョンと AWS アカウント番号<awsaccountid>に置き換えてください。

import boto3 import json import time REGION = "<Region>" CP_ENDPOINT_URL = f"https://bedrock-agentcore-control.{REGION}.amazonaws.com" DP_ENDPOINT_URL = f"https://bedrock-agentcore.{REGION}.amazonaws.com" # Update the accountId to reflect the correct S3 path. S3_BUCKET_NAME = "DOC-EXAMPLE-BUCKET" bedrock_agentcore_control_client = boto3.client( 'bedrock-agentcore-control', region_name=REGION, endpoint_url=CP_ENDPOINT_URL ) bedrock_agentcore_client = boto3.client( 'bedrock-agentcore', region_name=REGION, endpoint_url=DP_ENDPOINT_URL ) unique_name = f"s3InteractionEnv_{int(time.time())}" create_response = bedrock_agentcore_control_client.create_code_interpreter( name=unique_name, description="Combined test code sandbox", executionRoleArn="arn:aws:iam::123456789012:role/S3InteractionRole", networkConfiguration={ "networkMode": "SANDBOX" } ) code_interpreter_id = create_response['codeInterpreterId'] print(f"Created custom interpreter ID: {code_interpreter_id}") session_response = bedrock_agentcore_client.start_code_interpreter_session( codeInterpreterIdentifier=code_interpreter_id, name="combined-test-session", sessionTimeoutSeconds=1800 ) session_id = session_response['sessionId'] print(f"Created session ID: {session_id}") print(f"Downloading CSV generation script from S3") command_to_execute = f"aws s3 cp s3://{S3_BUCKET_NAME}/generate_csv.py ." response = bedrock_agentcore_client.invoke_code_interpreter( codeInterpreterIdentifier=code_interpreter_id, sessionId=session_id, name="executeCommand", arguments={ "command": command_to_execute } ) for event in response["stream"]: print(json.dumps(event["result"], default=str, indent=2)) print(f"Executing the CSV generation script") response = bedrock_agentcore_client.invoke_code_interpreter( codeInterpreterIdentifier=code_interpreter_id, sessionId=session_id, name="executeCommand", arguments={ "command": "python generate_csv.py 5 10" } ) for event in response["stream"]: print(json.dumps(event["result"], default=str, indent=2)) print(f"Uploading generated artifact to S3") command_to_execute = f"aws s3 cp generated_data.csv s3://{S3_BUCKET_NAME}/output_artifacts/" response = bedrock_agentcore_client.invoke_code_interpreter( codeInterpreterIdentifier=code_interpreter_id, sessionId=session_id, name="executeCommand", arguments={ "command": command_to_execute } ) for event in response["stream"]: print(json.dumps(event["result"], default=str, indent=2)) print(f"Stopping the code interpreter session") stop_response = bedrock_agentcore_client.stop_code_interpreter_session( codeInterpreterIdentifier=code_interpreter_id, sessionId=session_id ) print(f"Deleting the code interpreter") delete_response = bedrock_agentcore_control_client.delete_code_interpreter( codeInterpreterId=code_interpreter_id ) print(f"Code interpreter status from response: {delete_response['status']}") print(f"Clean up completed, script run successful")

この例では、以下の方法を示します。

  • 実行ロールを使用してカスタムコードインタープリタを作成する

  • ネットワークアクセスの設定 - Code Interpreter がパブリックインターネットに接続する必要がある場合は、PUBLIC モードを選択します。Code Interpreter が Amazon S3 に制限されたアクセスを必要とする場合は、SANDBOX モードを選択します。

  • Code Interpreter 環境と S3 の間でファイルをアップロードおよびダウンロードする

  • Code Interpreter 環境内でコマンドとスクリプトを実行する

  • 完了したらリソースをクリーンアップする