Skip to content

Bedrock Agent

This template demonstrates how to handle Amazon Bedrock Agent events using function-based actions.

Architecture

The template sets up:

  1. Amazon Bedrock Agent: Orchestrates user requests and identifies tool usage.
  2. AWS Lambda function: Resolves function calls using BedrockAgentFunctionResolver.
  3. Amazon DynamoDB table: Stores and retrieves items used by the agent.

Architecture diagram showing Amazon Bedrock Agent orchestrating user requests and triggering AWS Lambda functions to interact with a DynamoDB table.

Code

Deployment

Deploy the stack using:

mise run deploy agent

Features

  • Function-based Actions: Uses the @app.tool() decorator to expose functions to Bedrock Agents.
  • Automatic Parameter Mapping: Maps Bedrock Agent parameters directly to Python function arguments.
  • Type Safety: Uses Pydantic models for data validation and camelCase alias conversion.
  • Observability: Integrated with AWS Lambda Powertools for logging, tracing, and metrics.

Usage

Define Tools

Use the @app.tool() decorator to define tools that the agent can call:

@app.tool(name="getItem", description="Gets item details by ID")
def get_item(item_id: str) -> dict:
    return handler.get_item(item_id)

Item model

Field Type Description
id UUID string Unique item identifier (auto-generated, 1-50 chars)
name string Human-readable item name (1-100 chars)
description string Human-readable item description (max 500 chars)

Environment variables

Variable Description Required Default
TABLE_NAME DynamoDB table name Yes -
SERVICE_NAME Powertools service name No bedrock-agent
METRICS_NAMESPACE Powertools metrics namespace No BedrockAgent
LOG_LEVEL Log level for the Lambda Logger No INFO