Skip to content

Bedrock Agent

create_item(item_id, name, description=None)

Create a new item.

Parameters:

Name Type Description Default
item_id str

Unique identifier for the item.

required
name str

Name of the item.

required
description str | None

Optional description of the item.

None

Returns:

Type Description
dict

The created item details.

Source code in templates/agent/handler.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@tracer.capture_method
@app.tool(name="createItem", description="Creates a new item with name and optional description")
def create_item(item_id: str, name: str, description: str | None = None) -> dict:
    """Create a new item.

    Args:
        item_id: Unique identifier for the item.
        name: Name of the item.
        description: Optional description of the item.

    Returns:
        The created item details.
    """
    logger.info("Creating item", extra={"itemId": item_id})
    try:
        item = Item(id=item_id, name=name, description=description).dump()
        repository.put_item(item)
        return item
    except Exception as error:
        logger.error("Failed to create item", extra={"itemId": item_id}, exc_info=error)
        return {"error": f"Failed to create item with ID '{item_id}'"}

get_item(item_id)

Retrieve an item by its ID.

Parameters:

Name Type Description Default
item_id str

The unique identifier of the item.

required

Returns:

Type Description
dict

The item details or an error message.

Source code in templates/agent/handler.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@tracer.capture_method
@app.tool(name="getItem", description="Gets item details by ID")
def get_item(item_id: str) -> dict:
    """Retrieve an item by its ID.

    Args:
        item_id: The unique identifier of the item.

    Returns:
        The item details or an error message.
    """
    logger.info("Retrieving item", extra={"itemId": item_id})
    try:
        item = repository.get_item(item_id)
        if not item:
            return {"error": f"Item {item_id} not found"}
        return Item.model_validate(item).dump()
    except Exception as error:
        logger.error("Failed to get item", extra={"itemId": item_id}, exc_info=error)
        return {"error": f"Failed to get item with ID '{item_id}'"}

main(event, context)

Lambda entry point for the Bedrock Agent handler.

Parameters:

Name Type Description Default
event dict | BedrockAgentEvent

The Bedrock Agent event.

required
context LambdaContext

The Lambda execution context.

required

Returns:

Type Description
dict

The Bedrock Agent function response.

Source code in templates/agent/handler.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@logger.inject_lambda_context
@tracer.capture_lambda_handler
@metrics.log_metrics
def main(event: dict | BedrockAgentEvent, context: LambdaContext) -> dict:
    """Lambda entry point for the Bedrock Agent handler.

    Args:
        event: The Bedrock Agent event.
        context: The Lambda execution context.

    Returns:
        The Bedrock Agent function response.
    """
    return app.resolve(event, context)

Item

Bases: Entity

Model representing an item managed by the agent.

Source code in templates/agent/models.py
 6
 7
 8
 9
10
class Item(Entity):
    """Model representing an item managed by the agent."""

    name: str = Field(description="Name of the item.", min_length=1, max_length=100)
    description: str | None = Field(description="Description of the item.", default=None, max_length=500)

Settings

Bases: CommonSettings

Configuration settings for the Bedrock Agent Lambda function.

Source code in templates/agent/settings.py
 6
 7
 8
 9
10
11
class Settings(CommonSettings):
    """Configuration settings for the Bedrock Agent Lambda function."""

    table_name: str = Field(description="Name of the DynamoDB table to store agent items.")
    service_name: str = "bedrock-agent"
    metrics_namespace: str = "BedrockAgent"