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
Wasmで社内ツールを作って配布しよう
askua
0
150
PdM業務における使い分け
shinshiro
0
670
機械学習を「社会実装」するということ 2025年夏版 / Social Implementation of Machine Learning July 2025 Version
moepy_stats
1
1.4k
メモ整理が苦手な者による頑張らないObsidian活用術
optim
0
150
なぜAI時代に 「イベント」を中心に考えるのか? / Why focus on "events" in the age of AI?
ytake
2
800
地域コミュニティへの「感謝」と「恩返し」 / 20250726jawsug-tochigi
kasacchiful
0
100
【CEDEC2025】LLMを活用したゲーム開発支援と、生成AIの利活用を進める組織的な取り組み
cygames
PRO
1
1.6k
Snowflake のアーキテクチャは本当に筋がよかったのか / Data Engineering Study #30
indigo13love
0
280
分散トレーシングによる コネクティッドカーのデータ処理見える化の試み
thatsdone
0
270
CSPヘッダー導入で実現するWebサイトの多層防御:今すぐ試せる設定例と運用知見
llamakko
1
260
Ktor + Google Cloud Tasks/PubSub におけるOTel Messaging計装の実践
sansantech
PRO
1
330
データエンジニアがクラシルでやりたいことの現在地
gappy50
3
660
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
760
Building a Scalable Design System with Sketch
lauravandoore
462
33k
The Invisible Side of Design
smashingmag
301
51k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
Writing Fast Ruby
sferik
628
62k
A Tale of Four Properties
chriscoyier
160
23k
The World Runs on Bad Software
bkeepers
PRO
70
11k
Raft: Consensus for Rubyists
vanstee
140
7k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Docker and Python
trallard
45
3.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
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.