AutoGen Tutorial (Python): deploying to AWS Lambda for advanced developers
This tutorial shows how to package an AutoGen-based Python agent for AWS Lambda and invoke it through a handler that returns structured JSON. You need this when you want a lightweight, serverless agent endpoint for internal tools, event-driven workflows, or API-backed automation without running a long-lived service.
What You'll Need
- •Python 3.11 locally
- •AWS account with permission to create:
- •Lambda functions
- •IAM roles
- •CloudWatch log groups
- •An OpenAI API key exported as
OPENAI_API_KEY - •
autogen-agentchatandautogen-extinstalled locally - •
boto3for local testing if you want to invoke Lambda from Python - •AWS CLI configured with credentials
- •A deployment bundle built on Amazon Linux-compatible binaries, or a container-based Lambda deployment
Step-by-Step
- •Start with a minimal AutoGen agent that can run inside Lambda.
The key constraint is that Lambda handlers must be stateless and fast, so keep the agent creation inside a helper function and return plain JSON.
import os
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient
def build_agent() -> AssistantAgent:
model_client = OpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.environ["OPENAI_API_KEY"],
)
return AssistantAgent(
name="lambda_assistant",
model_client=model_client,
system_message="You are a concise assistant for enterprise automation.",
)
async def run_agent(prompt: str) -> str:
agent = build_agent()
result = await agent.on_messages([TextMessage(content=prompt, source="user")], cancellation_token=None)
return result.chat_message.content
- •Add the Lambda handler wrapper.
AWS Lambda expects a synchronoushandler(event, context)entrypoint, so useasyncio.run()to bridge into the async AutoGen call.
import json
def lambda_handler(event, context):
prompt = event.get("prompt", "Summarize why serverless agents are useful.")
output = asyncio.run(run_agent(prompt))
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(
{
"input": prompt,
"output": output,
}
),
}
- •Package dependencies for Lambda.
For production, do not rely on your local site-packages. Build into a clean directory and zip only what Lambda needs.
mkdir -p build/python
python -m pip install --upgrade pip
python -m pip install \
--target build/python \
autogen-agentchat \
autogen-ext \
openai
cp lambda_function.py build/
cd build
zip -r ../autogen-lambda.zip .
cd ..
- •Create the Lambda function with an execution role.
Give it basic logging permissions and set the handler tolambda_function.lambda_handler.
aws iam create-role \
--role-name autogen-lambda-role \
--assume-role-policy-document '{
"Version":"2012-10-17",
"Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]
}'
aws iam attach-role-policy \
--role-name autogen-lambda-role \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
ROLE_ARN=$(aws iam get-role --role-name autogen-lambda-role --query 'Role.Arn' --output text)
aws lambda create-function \
--function-name autogen-agent-lambda \
--runtime python3.11 \
--handler lambda_function.lambda_handler \
--zip-file fileb://autogen-lambda.zip \
--role "$ROLE_ARN" \
--timeout 30 \
--memory-size 512 \
--environment Variables="{OPENAI_API_KEY=$OPENAI_API_KEY}"
- •Invoke the function and inspect the response.
If the packaging is correct, you should get back a JSON body containing both your input prompt and the model output.
aws lambda invoke \
--function-name autogen-agent-lambda \
--payload '{"prompt":"List three operational risks when deploying AI agents in banking."}' \
response.json
cat response.json
- •Tighten it for production use.
The big Lambda-specific issues are cold start time, dependency size, and external API latency, so keep the agent small and deterministic where possible.
import json
def lambda_handler(event, context):
try:
prompt = event.get("prompt", "")
if not prompt:
return {"statusCode": 400, "body": json.dumps({"error": "prompt is required"})}
output = asyncio.run(run_agent(prompt))
return {"statusCode": 200, "body": json.dumps({"output": output})}
except Exception as e:
return {"statusCode": 500, "body": json.dumps({"error": str(e)})}
Testing It
First verify that the Lambda logs show no import errors; most deployment failures here come from missing wheels or incompatible native packages. Then confirm the function returns valid JSON and that the model response appears in under your configured timeout.
If you get Runtime.ImportModuleError, check that lambda_function.py is at the root of the zip file and that dependencies were installed into a compatible target directory. If invocation works locally but fails in AWS, inspect CloudWatch logs for missing environment variables or outbound network issues.
For local sanity checks before deployment, run the handler directly:
if __name__ == "__main__":
print(lambda_handler({"prompt": "Explain idempotency in one sentence."}, None))
Next Steps
- •Move secrets from environment variables to AWS Secrets Manager and load them at runtime.
- •Add structured tool calls so the agent can query internal APIs instead of only generating text.
- •Put API Gateway in front of Lambda if you need authenticated HTTP access from other services.
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