This document provides a comprehensive guide for implementing commercial customer usage tracking for this AWS Bedrock Lambda Proxy service. The solution leverages AWS API Gateway with a DynamoDB-based token management system to track customer usage with Bearer token authentication, following AWS-native patterns and community standards.
Key Components:
This solution allows you to:
The architecture diagram below illustrates how the components interact:
Architecture Components:
The api-gateway-setup.sh
script automates the creation of:
Setup Steps:
# Open the file
nano api-gateway-setup.sh
# Update these values
LAMBDA_ARN="arn:aws:lambda:REGION:ACCOUNT_ID:function:bedrock-proxy"
REGION="us-east-1" # Your AWS region
chmod +x api-gateway-setup.sh
./api-gateway-setup.sh
The customer-management.py
script provides a CLI tool for managing customers and their Bearer tokens.
Setup Steps:
pip install boto3
aws configure
bedrock_customers
on first run.Deploy the CloudFormation template (reporting-setup.yml
) to set up the analytics infrastructure:
aws cloudformation create-stack \
--stack-name bedrock-usage-analytics \
--template-body file://reporting-setup.yml \
--capabilities CAPABILITY_IAM
Once deployment is complete, note the S3 bucket name and Glue database name from the outputs.
bedrock_usage_db
databaseWhen onboarding a new customer:
python customer-management.py create \
--name "Customer Name" \
--email "customer@example.com"
The script will output a customer ID and Bearer token. Provide this token to your customer along with integration instructions.
# Generate a secure token
TOKEN=$(openssl rand -base64 32)
# Create customer with specified token
python customer-management.py create \
--name "Customer Name" \
--email "customer@example.com" \
--token "$TOKEN"
To view all registered customers:
python customer-management.py list
This command displays all customers stored in your DynamoDB table, including their customer IDs, names, and email addresses.
To retrieve usage statistics for a specific customer:
python customer-management.py usage \
--customer-id "12345678-1234-1234-1234-123456789012" \
--start-date "2023-04-01" \
--end-date "2023-04-30"
This retrieves the number of API calls made by the customer within the specified date range, useful for billing purposes.
The reporting infrastructure automatically:
In Amazon QuickSight:
bedrock_usage_db
databaseA typical monthly billing workflow:
# Using the script
python customer-management.py usage \
--customer-id "CUSTOMER_ID" \
--start-date "BILLING_START" \
--end-date "BILLING_END"
# Or query the data in Athena/QuickSight
// JavaScript Example (Browser/Node.js)
async function callBedrockAPI(prompt) {
const apiEndpoint = 'https://your-api-gateway-url/prod/bedrock';
const authToken = 'your-customer-token-here';
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`
},
body: JSON.stringify({
// Required: specify which model to use
modelId: 'anthropic.claude-3-sonnet-20240229-v1:0',
// Claude-specific parameters
anthropic_version: 'bedrock-2023-05-31',
max_tokens: 1000,
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: prompt
}
]
}
]
})
});
if (!response.ok) {
throw new Error(`API request failed: ${response.status}`);
}
return await response.json();
}
// Example usage
// callBedrockAPI("What is the capital of France?")
// .then(result => console.log(result))
// .catch(error => console.error(error));
import requests
def call_bedrock_api(prompt):
api_endpoint = "https://your-api-gateway-url/prod/bedrock"
auth_token = "your-customer-token-here"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {auth_token}"
}
payload = {
"modelId": "anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
]
}
response = requests.post(api_endpoint, headers=headers, json=payload)
response.raise_for_status() # Raise exception for 4XX/5XX responses
return response.json()
# Example usage
# try:
# result = call_bedrock_api("What is the capital of France?")
# print(result)
# except Exception as e:
# print(f"Error: {e}")
curl -X POST https://your-api-gateway-url/prod/bedrock \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-customer-token-here" \
-d '{
"modelId": "anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is the capital of France?"
}
]
}
]
}'
# Process for rotating a customer's token
# 1. Generate a new secure token
NEW_TOKEN=$(openssl rand -base64 32)
# 2. Update the token in your database
python customer-management.py update-token \
--customer-id "CUSTOMER_ID" \
--token "$NEW_TOKEN"
# 3. Provide to customer
# 4. After customer confirms migration, invalidate old token
# Update customer rate limits in DynamoDB
python customer-management.py update-limits \
--customer-id "CUSTOMER_ID" \
--daily-limit 1000
future-enhancements.md for consideration. This includes e.g.:
Note: This solution follows AWS best practices and community standards for implementing a commercial model for AWS Bedrock access. It is designed to be low-maintenance, scalable, and compatible with your existing Lambda function’s Bearer token authentication.