Module 11 Lesson 2: Lambda-Backed Tools
Connecting to Code. How to use AWS Lambda to execute the actual logic behind your agent's action groups.
Executing the Logic: Lambda Tools
When a Bedrock Agent decides to call a tool (e.g., get_user_email), it sends a request to an AWS Lambda function. The Lambda function executes the code (searching a DB, calling an external API) and returns the result to the agent.
1. The Lambda Event Structure
Bedrock sends a specific JSON "Event" to your Lambda. Your code must parse it to find the Action Group and Parameters.
import json
def lambda_handler(event, context):
action_group = event['actionGroup']
function = event['function']
parameters = event['parameters']
# Logic based on the function name
if function == 'get_order_status':
order_id = parameters[0]['value']
# Call your database here...
result = f"Order {order_id} is currently: SHIPPED"
# Standard Bedrock Response Format
response = {
'actionGroup': action_group,
'function': function,
'functionResponse': {
'responseBody': {
'TEXT': {
'body': result
}
}
}
}
return {'response': response}
2. Security and Permissions
- The Agent must have permission to
lambda:InvokeFunction. - The Lambda must have a Resource-based Policy allowing the Bedrock service principal (
bedrock.amazonaws.com) to call it.
3. Visualizing the Execution Flow
graph LR
Agent[Agent Brain] -->|Request Params| L[AWS Lambda]
L -->|DB Query| DB[(Postgres)]
DB -->|Data| L
L -->|Format: TEXT| Agent
Agent -->|Synthesize| User[Human Answer]
💡 Guidance for Learners
Keep your Lambda functions Small. One Lambda can handle 5-10 different "Tools" in an Action Group using if/else logic. This makes management much easier than having 50 separate Lambda functions.
Summary
- Lambda provides the "Muscle" for Bedrock Agent actions.
- You must follow a specific JSON Response Schema for Bedrock to understand the result.
- Permissions must be configured on both the Agent and the Lambda.
- Lambda allows your AI to interact with any AWS service or external API.