This repository contains an AWS Lambda function that processes restaurant data using AWS Bedrock Agent Runtime.
The lambda function (lambda_function.py) serves as an API endpoint that:
- Accepts queries about data
- Loads relevant business data files from S3 based on configuration
- Processes requests through AWS Bedrock Agent Runtime
- Returns AI-generated responses with insights
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ API Gateway │───▶│ Lambda Function │───▶│ Bedrock Agent │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ S3 Storage │
│ (Data Files) │
└─────────────────┘
The function uses two external configuration files that are loaded at runtime:
Defines the S3 bucket and path structure for data files.
Maps business information types to their corresponding file prefixes.
Create the following configuration files in your deployment package:
s3:
bucket_name: "your-data-bucket-name"
base_path: "-data"{
"Customer Hub": "clients",
"Help Centre": "support",
"Loyalty": "rewards",
"Live Report Hub": "analytics",
"Activities": "events"
}Set the following environment variables in your Lambda function:
AGENT_ID: Your Bedrock Agent IDAGENT_ALIAS_ID: Your Bedrock Agent Alias ID
Ensure your S3 bucket follows this structure:
s3://your-bucket-name/restaurant-data/
├── business-id-1/
│ ├── x.csv
│ ├── z.csv
│ └── c.csv
├── business-id-2/
│ ├── x.csv
│ └── c.csv
└── ...
{
"query": "What were my top selling items last month?",
"business_id": ["business-123", "business-456"],
"information_type": ["x", "z"],
"session_id": "optional-session-id",
"enable_trace": false,
"end_session": false,
"memory_id": "optional-memory-id"
}{
"statusCode": 200,
"body": {
"response": "AI-generated response about your data",
"session_id": "session-id",
"trace": {...}
}
}├── lambda_function.py # Main Lambda handler
├── util.py # Utility functions
├── s3_config.yaml # S3 configuration (gitignored)
├── file_type_mappings.json # File type mappings (gitignored)
├── .gitignore # Git ignore rules
└── README.md # This file
boto3: AWS SDK for Pythonpyyaml: YAML parsing- AWS Bedrock Agent Runtime permissions
- S3 read permissions for the configured bucket
- Configuration files are gitignored as they may contain sensitive information
- The function validates all inputs and handles errors gracefully
- S3 paths are constructed securely to prevent path traversal attacks
The function includes comprehensive error handling for:
- Invalid JSON requests
- Missing business IDs
- Empty queries
- Configuration file loading errors
- S3 access issues
- Bedrock service errors
- Create the configuration files with your test data
- Install dependencies:
pip install boto3 pyyaml - Test with sample events
- Package the Lambda function with configuration files
- Upload to AWS Lambda
- Configure environment variables
- Set up API Gateway trigger
s3:
bucket_name: "my-data-bucket"
base_path: "business-data"{
"Orders": "sales",
"Menus": "catalog",
"Payment": "transactions",
"Promo": "discounts",
"Inventory": "stock",
"Customer Hub": "clients",
"Help Centre": "support",
"Loyalty": "rewards",
"Live Report Hub": "analytics",
"Activities": "events"
}- Configuration files not found: Ensure
s3_config.yamlandfile_type_mappings.jsonare in the Lambda deployment package - S3 access denied: Verify Lambda execution role has S3 read permissions
- Bedrock access denied: Ensure Lambda has Bedrock Agent Runtime permissions
- Invalid b IDs: Check that business IDs exist in S3 and match the expected format
The function provides detailed CloudWatch logs including:
- Configuration loading status
- S3 file loading progress
- Bedrock agent responses
- Error details and stack traces