AutoGen Tutorial (TypeScript): deploying to AWS Lambda for advanced developers
This tutorial shows how to package an AutoGen TypeScript agent workflow as an AWS Lambda handler, then invoke it through API Gateway or direct Lambda calls. You need this when your agent should run on demand, stay stateless, and fit into a serverless deployment model with tight operational control.
What You'll Need
- •Node.js 20+
- •AWS account with permission to create:
- •Lambda
- •IAM roles
- •CloudWatch Logs
- •API Gateway, if you want HTTP access
- •An OpenAI API key
- •TypeScript 5.x
- •These packages:
- •
@autogen/agentchat - •
openai - •
zod - •
esbuild - •
aws-sdkis not required for the Lambda runtime in this example
- •
- •A bundler or deployment tool:
- •
esbuildfor local packaging - •or AWS CDK / SST if you want infrastructure as code
- •
Step-by-Step
- •Create a small Lambda-friendly AutoGen entrypoint.
Keep the agent stateless per invocation and avoid global mutable conversation state. For Lambda, that means constructing the model client and agents inside the handler or in a cached module scope that is safe to reuse.
import { z } from "zod";
import { AssistantAgent, UserProxyAgent } from "@autogen/agentchat";
import { OpenAIChatCompletionClient } from "@autogen/agentchat";
const eventSchema = z.object({
prompt: z.string().min(1),
});
export const handler = async (event: unknown) => {
const { prompt } = eventSchema.parse(
typeof event === "string" ? JSON.parse(event) : event,
);
const modelClient = new OpenAIChatCompletionClient({
model: "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY!,
});
const assistant = new AssistantAgent({
name: "assistant",
modelClient,
systemMessage: "You are a concise banking operations assistant.",
});
const user = new UserProxyAgent({
name: "user",
humanInputMode: "NEVER",
});
const result = await user.initiateChat(assistant, {
message: prompt,
maxTurns: 2,
});
return {
statusCode: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({ result }),
};
};
- •Add a local build config that produces a single Lambda bundle.
Lambda works best when you ship one compiled file plus external environment variables. Useesbuildso your deployment artifact stays small and predictable.
import { build } from "esbuild";
await build({
entryPoints: ["src/handler.ts"],
bundle: true,
platform: "node",
target: "node20",
outfile: "dist/index.js",
sourcemap: true,
minify: false,
});
- •Wire up package scripts and TypeScript settings.
The important part is making sure Node ESM/CJS mode matches your deployment target. If you are deploying CommonJS output, keep the config simple and let esbuild handle bundling.
{
"name": "autogen-lambda",
"private": true,
"type": "module",
"scripts": {
"build": "tsx build.ts",
"zip": "cd dist && zip -r ../lambda.zip ."
},
"dependencies": {
"@autogen/agentchat": "^0.4.0",
"openai": "^4.0.0",
"zod": "^3.23.8"
},
"devDependencies": {
"esbuild": "^0.25.0",
"tsx": "^4.19.0",
"typescript": "^5.6.3"
}
- •Deploy the bundle to AWS Lambda with environment variables.
Set the handler toindex.handlerif your bundled file isdist/index.js. Give Lambda enough memory and timeout for model calls; for most agent requests, start with 1024 MB and a 30-second timeout.
npm install
npm run build
cd dist
zip -r ../lambda.zip .
aws lambda create-function \
--function-name autogen-ts-agent \
--runtime nodejs20.x \
--handler index.handler \
--zip-file fileb://../lambda.zip \
--role arn:aws:iam::123456789012:role/lambda-execution-role \
--timeout 30 \
| jq .
aws lambda update-function-configuration \
--function-name autogen-ts-agent \
| jq .
- •Test it with a direct invoke before adding API Gateway.
This isolates packaging issues from HTTP integration issues. Use a simple payload and inspect CloudWatch logs if the invocation fails.
aws lambda invoke \
--function-name autogen-ts-agent \
| jq .
cat response.json
- •Add a thin production guardrail around prompt size and errors.
Lambda failures should be deterministic and easy to debug, so validate input early and return structured errors instead of throwing raw stack traces to callers.
export const handler = async (event: unknown) => {
typeof event === "string" ? JSON.parse(event) : event,
return {
statusCode: e instanceof Error ? e.name === "" ? "" : e.message : "",
headers: { "content-type": "" },
bodyJSON.stringify({ error:return {
statusCode:400,
body:"Invalid request payload"};
};
Testing It
Start with direct Lambda invocation using a known-good prompt like “Summarize three risks in an insurance claims workflow.” Confirm that the response body contains the agent output and that CloudWatch logs show one clean turn through the chat flow.
If you get module resolution errors, inspect the bundled output in dist/index.js before touching AWS settings. If you get timeouts, increase memory first; Lambda CPU scales with memory, which usually helps model-call latency more than people expect.
For API Gateway testing, send a POST request with JSON like {"prompt":"..."}
and verify your handler returns 200 with valid JSON content-type headers.
Next Steps
- •Add tool calling for internal systems like policy lookup or claims status checks.
- •Move secrets to AWS Secrets Manager instead of plain environment variables.
- •Wrap this in CDK or SST so your Lambda, IAM role, log retention, and API Gateway are versioned together
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit