Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Bedrock AgentをサポートしたPowertools for AWS Lambdaによ...
Search
fujioka-a
June 25, 2025
Technology
0
130
Bedrock AgentをサポートしたPowertools for AWS Lambdaによる開発の効率化・省力化
Media-JAWS x JAWS-UG千葉支部 #2 幕張よ我々は帰ってきた!で発表した資料です(一部編集してます)
fujioka-a
June 25, 2025
Tweet
Share
Other Decks in Technology
See All in Technology
Recoil脱却の現状と挑戦
kirik
3
460
claude codeでPrompt Engineering
iori0311
0
530
VLMサービスを用いた請求書データ化検証 / SaaSxML_Session_1
sansan_randd
0
110
生成AIを活用した野球データ分析 - メジャーリーグ編 / Baseball Analytics for Gen AI
shinyorke
PRO
1
230
KCD Lima: eBee in Peru!
lizrice
0
110
増え続ける脆弱性に立ち向かう: 事前対策と優先度づけによる 持続可能な脆弱性管理 / Confronting the Rise of Vulnerabilities: Sustainable Management Through Proactive Measures and Prioritization
nttcom
1
210
【CEDEC2025】大規模言語モデルを活用したゲーム内会話パートのスクリプト作成支援への取り組み
cygames
PRO
1
320
人と生成AIの協調意思決定/Co‑decision making by people and generative AI
moriyuya
0
160
複数のGemini CLIが同時開発する狂気 - Jujutsuが実現するAIエージェント協調の新世界
gunta
13
3.8k
Power Automate のパフォーマンス改善レシピ / Power Automate Performance Improvement Recipes
karamem0
0
270
AI駆動開発 with MixLeap Study【大阪支部 #3】
lycorptech_jp
PRO
0
270
Datasets for Critical Operations by Dataform
kimujun
0
120
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Scaling GitHub
holman
461
140k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.6k
Navigating Team Friction
lara
187
15k
Designing Experiences People Love
moore
142
24k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
How to Ace a Technical Interview
jacobian
278
23k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
390
It's Worth the Effort
3n
185
28k
Thoughts on Productivity
jonyablonski
69
4.8k
Transcript
MEDIA-JAWS X JAWS-UG千葉支部#2 幕張よ我々は帰ってきた! 2025.6.25(火) Bedrock Agentをサポートした Powertools for AWS
Lambdaによる 開発の効率化・省力化
自己紹介 藤岡 敦史 前職:バックエンドエンジニア、 農業系や建設系のクライアントにて開発 現職:フォージビジョン所属、 インフラエンジニア、 フルスタックに多数のクライアントにて開発・構築 2025 All
AWS Certifications Engineer →ゴールドジャケット受け取れました!! 好きなAWSサービス Lambda
はじめに そもそも”Powertools”とは… ※もっと詳しく知りたい方は、ぜひ以下のAWS福井さんの資料を参照ください(個人的に神資料です) https://pages.awscloud.com/rs/112-TZM- 766/images/20221124_24th_ISV_DiveDeepSeminar_Lambda_Powertools.pdf Powertools • OSSライブラリ • ロギング、トレース、メトリクスなど、
ベストプラクティスの型・実装パターンがある程度 決まっている機能を手軽に導入できる →ボイラープレートを減らすことで、 開発速度やメンテナンス性を向上させられる • 以下の言語をサポート • Python • Java • TypeScript • .NET
従来のCore utilities • 以降の説明は、今回検証に使用したTypeScriptを前提に説明します • 以下がPowertoolsの主要機能(ユーティリティ)です ※言語によってユーティリティの充実度はマチマチです ユーティリティ 概要 トレース
X-rayと連携した処理のトレース ログ 構造化ロギング メトリクス カスタム名前空間を利用したメトリクス集計 Event Handler AppSync AppSync Eventからのパブリッシュ&サブスクライブ に対するイベントハンドリング ※他にも沢山ユーティリティはあるので、公式サイトを確認ください https://docs.powertools.aws.dev/lambda/typescript/latest/
最新のCore utilities • 今回新たに、Bedrock AgentからLambdaを呼び出しする際に必要とする、 Lambda側での決まった実装パターンがユーティリティ化された →自前実装せずPowertoolsを使用することで、実装が大きく効率化される! ユーティリティ 概要 トレース
X-rayと連携した処理のトレース ログ 構造化ロギング メトリクス カスタム名前空間を利用したメトリクス集計 Event Handler AppSync AppSync Eventからのパブリッシュ&サブスクライブ に対するイベントハンドリング Bedrock Agent Bedrock Agentからの呼び出し に対するイベントハンドリング ※2025.6.22時点で、Pythonと.NETはBedrock Agentとの連携が対応しているようです (Javaはまだ無いようです)
Event Handler(Bedrock Agent) とは • 今回の新機能は、Powertools公式に以下の概要図で説明されています。
Event Handler(Bedrock Agent) とは • 今回の新機能は、Powertools公式に以下の概要図で説明されています。 Bedrock AgentとのIN/OUT や、 tool
useのルーティング (適切なLambdaへの誘導)
とりあえず一回動かしてみよう • 検証のために、Bedrock Agent + Lambda + Powertoolsで、 Google Books
API を使った書籍推薦エージェントを作成しました • 全体として、以下のような流れで動きます
とりあえず一回動かしてみよう デモ
Lambda実装を確認しよう // importは割愛 const app = new BedrockAgentFunctionResolver({ logger });
// Bedrock Agent – Lambda間のリゾルバ // Toolsの定義(searchBooks) app.tool<{ keywords: string, author: string }>( async ({ keywords, author }, { event }) => { try { // Tool logic const books = await searchBooks(keywords, author, 10); return books.map(book => ({ ...book })); } catch (error) { logger.error('error search books', { error }); const {sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration} = event; return new BedrockFunctionResponse({ responseState: 'FAILURE', body: 'Error search books', sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration, }); } }, { // BedrockAgentFunctionResolverが、Tool useとして識別するメタデータ name: 'searchBooks', description: 'Search for books by author or keywords', } ); export const handler = async (event: unknown, context: Context) => { return app.resolve(event as BedrockAgentFunctionEvent, context); }; まずミニマム版から
Lambda実装を確認しよう // importは割愛 const app = new BedrockAgentFunctionResolver({ logger });
// Bedrock Agent – Lambda間のリゾルバ // Toolsの定義(searchBooks) app.tool<{ keywords: string, author: string }>( async ({ keywords, author }, { event }) => { try { // Tool logic const books = await searchBooks(keywords, author, 10); return books.map(book => ({ ...book })); } catch (error) { logger.error('error search books', { error }); const {sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration} = event; return new BedrockFunctionResponse({ responseState: 'FAILURE', body: 'Error search books', sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration, }); } }, { // BedrockAgentFunctionResolverが、Tool useとして識別するメタデータ name: 'searchBooks', description: 'Search for books by author or keywords', } ); export const handler = async (event: unknown, context: Context) => { return app.resolve(event as BedrockAgentFunctionEvent, context); }; まずミニマム版から ①中間のリゾルバを作成 ②Bedrock Agentからの入力をリゾルブ ③メタデータから一致するToolを特定(今回で言えば”searchBooks”) ④-1. リゾルバから受け渡されるパラメータを受け取って、処理実行 ④-2. Bedrock Agentへ、エラーをRaise
Powertoolsを使用しないと、以下が必要 (リゾルバが無いので、)Eventから1つ1つパラメータ取得するための実装 • アクショングループ • Tool名 などなど Bedrock AgentへRaiseするExceptionクラスなどを、 自前で作成・メンテする
そもそも、Bedrock Agentからのイベントが期待する形式なのか チェックしないといけない • Eventが期待する形式か • パラメータは適切か(バリデーション) Powertoolsを導入することで、 実装の削減のみ限らず、メンテナンス性や可読性も 向上することができる!
実際に完成したLambda(コアのLog、Trace、Metricsを含む) 前半 import type { Context } from 'aws-lambda'; import
{ BedrockAgentFunctionResolver, BedrockFunctionResponse } from '@aws-lambda-powertools/event-handler/bedrock-agent'; import type { BedrockAgentFunctionEvent } from '@aws-lambda-powertools/event-handler/types'; import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics'; import { Tracer } from '@aws-lambda-powertools/tracer'; import { searchBooks } from './bookModules.js'; import { logger } from './logger.js'; const tracer = new Tracer({serviceName: 'searchBooksAgent'}); const metrics = new Metrics({namespace: 'searchBooks‘, serviceName: 'searchBooksAgent'}); const app = new BedrockAgentFunctionResolver({ logger }); // Bedrock Agent – Lambda間のリゾルバ app.tool<{ keywords: string, author: string }>( async ({ keywords, author }, { event }) => { // 以降の全ロギングに対して、以下パラメータを表示 logger.appendKeys({ keywords, author, tool: 'searchBooks' }); logger.info('searchBooks called'); try { const books = await searchBooks(keywords, author, 10); metrics.addMetric('searchBooksSucceeded', MetricUnit.Count, 1); return books.map(book => ({ ...book })); } catch (error) { トレースとメトリクスを宣言 構造化ログへのパラメータ付与 ToolUse確認のためのメトリクス集計
実際に完成したLambda(コアのLog、Trace、Metricsを含む) 後半 } catch (error) { logger.error('error search books', {
error }); metrics.addMetric('searchBooksFailed', MetricUnit.Count, 1); const {sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration} = event; return new BedrockFunctionResponse({ responseState: 'FAILURE', body: 'Error search books', sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration, }); } }, { name: 'searchBooks', description: 'Search for books by author or keywords', } ); export const handler = async (event: unknown, context: Context) => { logger.logEventIfEnabled(event); logger.setCorrelationId((event as BedrockAgentFunctionEvent).sessionId); logger.appendKeys({ requestId: context.awsRequestId }); return app.resolve(event as BedrockAgentFunctionEvent, context); }; Toolがエラーした場合にメトリクス へインクリメント (アラートと連携などできる) 複数サービス間のトレースをしやす いように、CorrelationIdを ロガーに設定 任意のキーをロガーに設定 (今回はrequestId)
LOG (Log group) • 構造的なログが得られる • 指定したキー(Ex. correlation_id, requestId)や、 パラメータ(tool,
keywords, author(今回は指定してないので無し))を自動的に出力 { "level": "DEBUG", "message": "Searching books", "timestamp": "2025-06-23T11:56:24.726Z", "service": "book-search-agent", "sampling_rate": 0, "xray_trace_id": "1-685940e8-694b07215e21870b3ea377b4", "correlation_id": "228342203262808", "requestId": "a6bc05c2-20e9-419d-ba75-f5497abcaa0b", "keywords": "[AWS 生成AI, 初心者向け, 機械学習入門]", "tool": "searchBooks", "maxResults": 10 }
Metrics (カスタム名前空間へのPUT) • Tool実行のカウントや、処理内部の部分的な実行時間(Duration)などを メトリクス化することができる • アラームやダッシュボードなどと連携することで、エージェントの運用に役立つ
TRACE (AWS X-ray) • トレースは重要。有効化しておきましょう。 • 複数のToolを構築している場合に、どのToolが呼ばれているか追跡しやすい • AgentとTool、外部APIなどとの問題切り分けに強力
まとめ • PowertoolsでBedrock Agentがサポートされるようになった • 導入することで、大幅な実装手間を削減することができ、 開発生産性に加えて、メンテナンス性や可読性を向上させることができる • さらに、従来のユーティリティ(トレース、ログ、メトリクスなど)と組み合わせることで、 Tool
の開発や運用を快適に行うことができる 生成AIエージェントの開発・運用を加速させることができる!! Powertoolsを積極活用しましょう!!
ご清聴ありがとうございました Thank you for your listening.