2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Bedrock AgentCoreのローカルでの起動からRuntimeのデプロイまで - MacBook(M2)

Posted at

Supershipの名畑です。うすた京介ワールド展に行ってきました。セクシーコマンドー外伝 すごいよ!!マサルさんから30年が経つということに驚き。日本のギャグ漫画はマサルさん登場前と登場後でがらりと変わった気すらします。

はじめに

前回に続いて今回もAIエージェントの話題です。

人工知能 (AI) エージェントは、環境と対話し、データを収集し、そのデータを使用して自己決定タスクを実行して、事前に決められた目標を達成するためのソフトウェアプログラムです。

参考:AI エージェントとは何ですか? - 人工知能のエージェントの説明 - AWS

AIエージェントについての変化著しい昨今ですが、AWSが先月、Amazon Bedrock AgentCoreを発表しました(以下 AgentCore)。

7 月 16 日、Amazon Bedrock AgentCore のプレビューを発表しました。これは、デベロッパーが Amazon Bedrock または他の場所でホストされているあらゆるフレームワークとモデルを使用して、AI エージェントを大規模、迅速、安全にデプロイおよび運用するのに役立つ、包括的な一連のエンタープライズグレードのサービスです。

参考:Amazon Bedrock AgentCore のご紹介: AI エージェントをあらゆる規模で安全にデプロイおよび運用する (プレビュー) | Amazon Web Services ブログ

せっかくなので自環境(MacBook M2)にてAgentCoreを動かしてみました。AgentCore Runtimeのデプロイまでです。
今回はその過程を記した記事となります。

公式の「5分で AI エージェントをデプロイ・ホスティングする – Amazon Bedrock AgentCore Runtime | Amazon Web Services」の内容に対して自環境に合わせた情報の補足をしたものです。

AWS上での設定

IAM権限

AgentCoreのためのIAMを先んじて用意しておきます。

Permissions for AgentCore Runtime - Amazon Bedrock AgentCoreの内容を元としました。

  • ポリシー
    • AgentCore Runtime execution roleのjsonをAmazonBedrockAgentCoreExecutionPolicyという名前で保存
  • ロール
    • AgentCore Runtime trust policyのjsonをカスタム信頼ポリシーとし、許可ポリシーとして上述のAmazonBedrockAgentCoreExecutionPolicyをアタッチし、AmazonBedrockAgentCoreExecutionRoleという名前で保存

以下2つの変更も行いました。

  • 123456789012を自分のアカウントIDに変更
  • agentNamemy_agentに変更

リージョンはus-east-1のままとしました。AgentCoreはプレビュー段階で、使えるリージョンが限られますので。

IAMについてはAWS画面上での設定でもAWS CLI経由での設定でもどちらでも良いかと思います。

モデル

今回用いるStrands Agents SDKのデフォルトモデルは現在はus.anthropic.claude-sonnet-4-20250514-v1:0です。

過去記事の通り、us-east-1におけるClaude Sonne 4のアクセス許可を取得しておく必要があります。

ローカル環境の構築

AWS CLI

AWS CLIのインストールと設定は完了している前提とします。
されていない方は、過去記事を参考にしてインストールしてください。リージョンをus-east-1とすることも忘れないようにします。

Python

Python 3.10以降が必須です。

% python3 --version
Python 3.12.2

コンテナエンジン

ローカルで動作させるにはコンテナエンジンが必要です。
インストールしていないと以下のエラーが出ます。

AgentCore requires one of the following container engines for local builds:
• Docker (any recent version, including Docker Desktop)
• Finch (Amazon's open-source container engine)
• Podman (compatible alternative to Docker)

To install:
• Docker: https://docs.docker.com/get-docker/
• Finch: https://github.com/runfinch/finch
• Podman: https://podman.io/getting-started/installation

Alternative: Use CodeBuild for cloud-based building (no container engine needed):
  agentcore launch  # Uses CodeBuild (default)

私はDockerを入れています。

% docker --version
Docker version 28.3.2, build 578ccf6

仮想環境

必須ではないですが、今回はvenvを用いて仮想環境でやります。
作業ディレクトリに移動した上で仮想環境の構築とactivateをします。

% python3 -m venv myenv  
% source myenv/bin/activate

SDK

AgentCore SDKStrands Agents SDKのうちの必要なものを入れます。

% pip install strands-agents bedrock-agentcore bedrock-agentcore-starter-toolkit
Collecting strands-agents
  Using cached strands_agents-1.4.0-py3-none-any.whl.metadata (12 kB)

以下略

バージョン確認をしておきます。

% pip list | grep -e strands-agents -e bedrock-agentcor -e bedrock-agentcore-starter-toolkit
bedrock-agentcore                       0.1.2
bedrock-agentcore-starter-toolkit       0.1.6
strands-agents                          1.4.0

コード

作業ディレクトリにて、以下のコードをmy_agent.pyとして保存します。呼び出される際にpromptを受け取るという内容になっています。

from bedrock_agentcore.runtime import BedrockAgentCoreApp
from strands import Agent

app = BedrockAgentCoreApp()
agent = Agent()

@app.entrypoint
def invoke(payload):
    user_message = payload.get("prompt")
    result = agent(user_message)
    return {"result": result.message}

if __name__ == "__main__":
    app.run()

以下の内容をrequirements.txtとして保存します。

strands-agents
bedrock-agentcore

AgentCoreの設定

agentcore configureコマンドを呼びます。
<IAM_ROLE_ARN> には実際の値を入れてください。

% agentcore configure --entrypoint my_agent.py -er <IAM_ROLE_ARN>

以下3つの質問をされますが、今回は全てEnterを押します。

  • ECR Repository URI (or press Enter to auto-create)
  • Path or Press Enter to use detected dependency file
  • Configure OAuth authorizer instead? (yes/no) [no]

すると以下のような表示がされて基本設定完了です。

╭──────────────────────────────────────────────── Bedrock AgentCore Configured ────────────────────────────────────────────────╮
│ Configuration Summary                                                                                                        │
│                                                                                                                              │
│ Name: my_agent                                                                                                               │
│ Runtime: Docker                                                                                                              │
│ Region: us-east-1                                                                                                            │
│ Account: アカウントID                                                                                                          │
│ Execution Role: IAMロールのARN                                                                                                 │
│ ECR: Auto-create                                                                                                             │
│ Authorization: IAM (default)                                                                                                 │
│                                                                                                                              │
│ Configuration saved to: 設定ファイルのパス                                                                                      │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Dockerfileも以下の内容で自動で作られています。

FROM public.ecr.aws/docker/library/python:3.12-slim
WORKDIR /app



COPY requirements.txt requirements.txt
# Install from requirements file
RUN pip install -r requirements.txt

以下略

AgentCoreのローカルでの呼び出し

まずagentcore launch --localを呼んでローカルで起動してみます。

% agentcore launch --local 
🏠 Launching Bedrock AgentCore (local mode)...
   • Build and run container locally
   • Requires Docker/Finch/Podman to be installed
   • Perfect for development and testing


略

Starting server at http://localhost:8080
Press Ctrl+C to stop

Configuration of aws_configurator not loaded, configurator already loaded
Attempting to instrument while already instrumented
INFO:     Started server process [1]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

この状態のまま、別ウィンドウを開いてagentcore invoke --localを呼び出してみます。プロンプトには「Hello」とだけ書きました。

% source myenv/bin/activate
% agentcore invoke --local '{"prompt": "Hello"}'

しばらく待つと以下のレスポンスが返ってきました。

"response": "{\"result\": {\"role\": \"assistant\", \"content\": [{\"text\": \"Hello! How are you doing today? Is there 
anything I can help you with?\"}]}}"

期待する内容になっています。

AgentCore Runtimeのデプロイ

--localをつけずに呼び出してみます。

% agentcore launch
🚀 Launching Bedrock AgentCore (codebuild mode - RECOMMENDED)...
   • Build ARM64 containers in the cloud with CodeBuild
   • No local Docker required (DEFAULT behavior)
   • Production-ready deployment

💡 Deployment options:
   • agentcore launch                → CodeBuild (current)
   • agentcore launch --local        → Local development
   • agentcore launch --local-build  → Local build + cloud deploy
   
略

╭─────────────────────────────────────────────── CodeBuild Deployment Complete ────────────────────────────────────────────────╮
│ CodeBuild ARM64 Deployment Successful!                                                                                       │
│                                                                                                                              │
│ Agent Name: my_agent                                                                                                         │

以下略

CodeBuild Deployment Completeと表示されています。

続けてこれまた --local をつけずにinvokeを呼んでみます。

% agentcore invoke '{"prompt": "Hello"}'

以下の通り、期待する結果が返ってきました。

"b'{\"result\": {\"role\": \"assistant\", \"content\": [{\"text\": \"Hello! How are you doing today? Is there anything I can
help you with?\"}]}}'"

Amazon ECRリポジトリAmazon Bedrock AgentCoreAgent Runtimeを見てみると今回の生成物がちゃんと存在していることもわかります。

agentcore_1.png

agentcore_2.png

不要であれば掃除しておきましょう。

最後に

変化は激しいですが、注目を集めている分だけ情報量もあるので、キャッチアップしていきます。

宣伝

SupershipのQiita Organizationを合わせてご覧いただけますと嬉しいです。他のメンバーの記事も多数あります。

Supershipではプロダクト開発やサービス開発に関わる方を絶賛募集しております。
興味がある方はSupership株式会社 採用サイトよりご確認ください。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?