AWS Lambda Integration
Build serverless AI functions on AWS Lambda with Antbase. Process events from API Gateway, S3, SQS, and more with intelligent model routing.
What this enables
- Serverless AI processing triggered by any AWS event
- Pay-per-invocation -- no idle costs
- Scale to thousands of concurrent AI requests
- Integrate with S3, DynamoDB, SQS, API Gateway, and more
Quick Start
1. Create a Lambda function (Node.js)
// index.mjs
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.ANTBASE_API_KEY,
baseURL: 'https://antbase.ai/v1',
});
export const handler = async (event) => {
const body = JSON.parse(event.body || '{}');
const response = await openai.chat.completions.create({
model: 'ant:auto',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: body.message || 'Hello!' },
],
max_tokens: 1024,
});
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
reply: response.choices[0].message.content,
}),
};
};2. Package and deploy
npm init -y && npm install openai
zip -r function.zip index.mjs node_modules
aws lambda create-function \
--function-name ai-handler \
--runtime nodejs20.x \
--handler index.handler \
--zip-file fileb://function.zip \
--role arn:aws:iam::ACCOUNT:role/lambda-role \
--timeout 30 \
--environment "Variables={ANTBASE_API_KEY=your-ant-api-key}"3. Add an API Gateway trigger
In the Lambda console, add an API Gateway trigger to expose your function as an HTTP endpoint.
Configuration
Python Lambda
# lambda_function.py
import json
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["ANTBASE_API_KEY"],
base_url="https://antbase.ai/v1",
)
def lambda_handler(event, context):
body = json.loads(event.get("body", "{}"))
response = client.chat.completions.create(
model="ant:auto",
messages=[{"role": "user", "content": body.get("message", "Hello")}],
max_tokens=1024,
)
return {
"statusCode": 200,
"body": json.dumps({
"reply": response.choices[0].message.content
})
}Use AWS Secrets Manager (recommended)
import boto3
import json
def get_api_key():
client = boto3.client('secretsmanager')
response = client.get_secret_value(SecretId='antbase/api-key')
return json.loads(response['SecretString'])['api_key']Usage Examples
S3 document processing
// Triggered by S3 upload
export const handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;
// Fetch the file from S3
const s3 = new S3Client({});
const obj = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
const text = await obj.Body.transformToString();
// Summarize with Antbase
const response = await openai.chat.completions.create({
model: 'ant:fast',
messages: [
{ role: 'system', content: 'Summarize this document in 3 bullet points.' },
{ role: 'user', content: text.slice(0, 10000) },
],
});
return { summary: response.choices[0].message.content };
};Virtual Models
| Model | Best for | Notes |
|---|---|---|
| ant:auto | General use | Picks optimal model per request |
| ant:fast | Event processing | Keep Lambda execution time short |
| ant:best | Complex analysis | Frontier models for quality output |
| ant:free | Development | Zero AI cost during testing |
| ant:reasoning | Analysis | Chain-of-thought reasoning |
| ant:coder | Code tasks | Code-optimized models |
See Virtual Models for the full list.
Troubleshooting
Function timeout
The default Lambda timeout is 3 seconds. Set it to at least 30 seconds for AI calls. Use ant:fast to minimize response time.
Cold start latency
Initialize the OpenAI client outside the handler function so it is reused across invocations. This avoids re-creating the client on every warm invocation.