冬休みの宿題シリーズ。
GenUで自前Agentセットアップ
やること
AgentCore + Strands + AgentCore Starter Kitのテスト実装を行う。
実装したAgentCoreランタイムは、GenUのAgentCore連携機能を使ってテストする。
環境
- リモート
- AWSアカウント
- Git系レポジトリ(何でも可)
- ローカル
- Appleシリコンのmac
- armであればM1-M4どれでもOK(intelの場合は追加作業が必要)
- iTerm2または適当なターミナル
- Docker Desktop
- KiroまたはVS Code
- Appleシリコンのmac
前提
- AWSプロファイルやCDKのbootstrapは構成済みであること。
- us-east-1(バージニア北部リージョン)を使用すること。
ステップバイステップ
1. 準備
-
適当なディレクトリを作り、GenUをクローンする。
-
% cd <親ディレクトリ> % git clone https://github.com/aws-samples/generative-ai-use-cases.git
-
-
Strandsコード用のディレクトリも作成しておく(レポジトリを作成してクローンするのでも可)。
-
% cd <親ディレクトリ> % mkdir agentcore-strands-trial
-
-
pyenv、poetry、node/npm、awscliをインストールしておく。
-
% brew install pyenv poetry node awscli -
ここでは全てbrewを使っているが、お好みの方法で構わない
-
-
Docker Desktopをインストールしておく。
-
AgentCore Starter KitでDockerコマンドが必要になるため
-
2. GenUのセットアップ
-
GenUディレクトリに移動する。
-
% cd <親ディレクトリ>/generative-ai-use-cases
-
-
pyenvとpoetryを有効化する。
-
% pyenv install 3.12.12 % pyenv local 3.12.12 % poetry init -
他のバージョンでも動作するかも知れないが、2025年12月現在で手元実績がある組み合わせを記載
-
-
requirements.txtを参考に、poetryの依存関係設定ファイル(pyproject.toml)に以下の依存関係情報を追記する。-
[tool.poetry.dependencies] python = "^3.12" mkdocs = "^1.6.1" mkdocs-material = { version = "^9.7.1", extras = ["imaging"] } mkdocs-minify-plugin = "^0.8.0" mkdocs-redirects = "^1.2.2" mkdocs-rss-plugin = "^1.17.7" mkdocs-github-admonitions-plugin = "^0.1.1" pymdown-extensions = "*" mkdocs-include-markdown-plugin = ">=7.1.5" mkdocs-static-i18n = { git = "https://github.com/maekawataiki/mkdocs-static-i18n.git", branch = "feat/default-subdirectory" }
-
-
Pythonの依存関係をインストールする。
-
% poetry install --no-root
-
-
TypeScriptの依存関係をインストールする。
-
% npm ci
-
3. AgentCore + Strandsの設定
-
Strandsディレクトリに移動する。
-
% cd <親ディレクトリ>/agentcore-strands-trial/
-
-
2. と同様にpyenvとpoetryを設定する。ただし、pyroject.tomlの依存関係は以下の通りとする。
-
[tool.poetry.dependencies] python = "^3.12" bedrock-agentcore-starter-toolkit = ">=0.2.5,<0.3.0" bedrock-agentcore = ">=1.1.1,<2.0.0" strands-agents = ">=1.20.0,<2.0.0" boto3 = ">=1.42.16,<2.0.0"
-
-
依存関係をインストールする。
-
% poetry install --no-root
-
-
Kiroを起動する。
-
% kiro .
-
-
エージェントコード(
weather_report.py)を作成する(参考1、参考2)。-
from strands import Agent, tool import argparse import json import random from bedrock_agentcore.runtime import BedrockAgentCoreApp from strands.models import BedrockModel # コード内のAppを初期化 app = BedrockAgentCoreApp() # カスタムツールの作成 @tool def weather(): """ Get the weather """ weather_options = ["sunny", "cloudy", "rainy", "snowy", "windy", "foggy", "stormy"] return random.choice(weather_options) model_id = "us.anthropic.claude-sonnet-4-5-20250929-v1:0" model = BedrockModel( model_id=model_id, ) agent = Agent( model = model, tools = [weather], system_prompt = "You're a helpful assistant. You can tell the weather." ) # エントリーポイントの作成 @app.entrypoint async def strands_agent_bedrock(payload): # GenUの仕様上、ストリーム処理に変更 """ Invoke the agent with a payload """ user_input = payload.get("prompt") stream_messages = agent.stream_async(user_input) async for message in stream_messages: if "event" in message: yield message if __name__ == "__main__": app.run() -
ここではModel IDをSonnet 4.5としている
-
-
AgentCore Starter Kitを使用して、エージェントをホストするDockerコンテナ(ランタイム上で動作)用のDockerfile作成コード(
agent_runtime.py)を作成する。-
from bedrock_agentcore_starter_toolkit import Runtime from boto3.session import Session boto_session = Session() region = boto_session.region_name agentcore_runtime = Runtime() agent_name = "weather_report" response = agentcore_runtime.configure( entrypoint = "weather_report.py", auto_create_execution_role = True, auto_create_ecr = True, requirements_file = "requirements.txt", region = region, agent_name = agent_name ) -
AgentCore Starter KitはAgentCoreの開発を容易にしてくれるSDKで、DockerfileやECRなど関連リソースも作成してくれる。なお、仕様上命名規則にハイフンは使えないので注意
-
-
Starter Kitの仕様上、
requirements.txtが必要なため、poetry設定に併せて以下の通り作成する。-
bedrock-agentcore-starter-toolkit>=0.2.5,<0.3.0 bedrock-agentcore>=1.1.1,<2.0.0 strands-agents>=1.20.0,<2.0.0 boto3>=1.42.16,<2.0.0
-
-
Dockerfileを生成する。
-
% poetry run python agentcore_runtime.py
-
-
AgentCore Starter Kitを使用して、エージェントをAgentCoreランタイムにデプロイする。
-
% poetry run agentcore launch -
コードを編集した場合は、このコマンドだけを再度実行すれば、同じARNでDockerコンテナ内のバージョンだけが上がっていく
-
4. GenUへのAgentCore設定追加
-
GenUディレクトリでKiroを起動する。
-
% cd <親ディレクトリ>/generative-ai-use-cases % kiro .
-
-
packages/cdk/parameter.tsの該当部分を編集する。
-
const envs: Record<string, Partial<StackInput>> = { // If you want to define an anonymous environment, uncomment the following and the content of cdk.json will be ignored. // If you want to define an anonymous environment in parameter.ts, uncomment the following and the content of cdk.json will be ignored. '': { // Parameters for anonymous environment // If you want to override the default settings, add the following modelRegion: 'us-east-1', // モデル用リージョンの指定 imageGenerationModelIds: [], videoGenerationModelIds: [], speechToSpeechModelIds: [], createGenericAgentCoreRuntime: true, // デフォルトエージェント用ランタイムの起動 agentCoreRegion: 'us-east-1', // AgentCoreリージョンの指定 agentCoreExternalRuntimes: [ // カスタムエージェント用ランタイムの指定 { name: 'weather_report', description: 'sample agent', arn: 'arn:aws:bedrock-agentcore:us-east-1:<アカウントID>:runtime/weather_report-XXXXXXXXXX', }, ], }, dev: { // Parameters for development environment }, staging: { // Parameters for staging environment }, prod: { // Parameters for production environment }, // If you need other environments, customize them as needed };
-
-
packages/cdk/cdk.jsonの該当部分を編集する。
-
{ "context": { ...(省略)... "createGenericAgentCoreRuntime": true, "agentCoreRegion": "us-east-1", "agentCoreExternalRuntimes": [ { "name": "weather_report", "description": "samnple agent", "arn": "arn:aws:bedrock-agentcore:us-east-1:<アカウントID>:runtime/weather_report-XXXXXXXXXX" } ], ...(省略)... } }
-
-
cdkをデプロイする。
-
% npx -w packages/cdk cdk deploy --all
-
5. テスト
-
GenUのWebURLを叩く。
-
CloudFormationスタック(デフォルトでは
GenerativeAiUseCasesStack)のOutputsから、WebURLを特定することができる。
なお、初回はユーザー登録が必要
-
-
デフォルトエージェント
松永久秀について教えて-
松永久秀の情報が返る
-
カスタムエージェント
今日の天気は?-
ランダムな天気が返る
まとめ
諸事情で積ん読状態になっていたAgentCore + Strandsだが、どうにか2025年中に動かすことができた。
いろんなところにサンプルはあるのだが、GenUとの相性もあり、やりたい構成で動かすのは思いのほか大変だった。
2026年になったらまた仕様が変わって改修が必要になってるかも知れない。。。
参考URL
様々な情報を参考にさせていただきました。感謝いたします。