AutoGen Tutorial (Python): deploying to AWS Lambda for beginners

By Cyprian AaronsUpdated 2026-04-21
autogendeploying-to-aws-lambda-for-beginnerspython

This tutorial shows how to package a basic AutoGen Python agent for AWS Lambda and trigger it through API Gateway. You need this when you want your agent logic behind a serverless endpoint instead of running it on a laptop or a long-lived server.

What You'll Need

  • Python 3.11
  • An AWS account with permission to create:
    • Lambda functions
    • IAM roles
    • API Gateway HTTP APIs
  • An OpenAI API key stored as an environment variable:
    • OPENAI_API_KEY
  • These Python packages:
    • autogen
    • boto3
  • AWS CLI installed and configured locally
  • Basic familiarity with AutoGen agents and AssistantAgent

Step-by-Step

  1. Start with a minimal AutoGen agent that can run inside Lambda.
    Keep the agent stateless and create it inside the handler so each invocation is independent.
import os
import autogen


def build_agent():
    llm_config = {
        "config_list": [
            {
                "model": "gpt-4o-mini",
                "api_key": os.environ["OPENAI_API_KEY"],
            }
        ],
        "temperature": 0,
    }

    return autogen.AssistantAgent(
        name="lambda_assistant",
        llm_config=llm_config,
    )
  1. Add the Lambda handler that accepts an HTTP request body, sends it to AutoGen, and returns the response.
    This version expects JSON like {"message": "Explain S3 lifecycle policies"}.
import json
import os
import autogen


def build_agent():
    llm_config = {
        "config_list": [
            {
                "model": "gpt-4o-mini",
                "api_key": os.environ["OPENAI_API_KEY"],
            }
        ],
        "temperature": 0,
    }

    return autogen.AssistantAgent(
        name="lambda_assistant",
        llm_config=llm_config,
    )


def lambda_handler(event, context):
    body = json.loads(event.get("body") or "{}")
    message = body.get("message", "").strip()

    if not message:
        return {
            "statusCode": 400,
            "headers": {"Content-Type": "application/json"},
            "body": json.dumps({"error": "Missing 'message' in request body"}),
        }

    agent = build_agent()
    reply = agent.generate_reply(messages=[{"role": "user", "content": message}])

    return {
        "statusCode": 200,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps({"reply": reply}),
    }
  1. Package dependencies in a Lambda-friendly way.
    The simplest path for beginners is to install into a local folder, zip it, then upload that zip to Lambda.
mkdir -p lambda_app
cd lambda_app

cat > app.py <<'EOF'
import json
import os
import autogen


def build_agent():
    llm_config = {
        "config_list": [
            {
                "model": "gpt-4o-mini",
                "api_key": os.environ["OPENAI_API_KEY"],
            }
        ],
        "temperature": 0,
    }

    return autogen.AssistantAgent(
        name="lambda_assistant",
        llm_config=llm_config,
    )


def lambda_handler(event, context):
    body = json.loads(event.get("body") or "{}")
    message = body.get("message", "").strip()

    if not message:
        return {
            "statusCode": 400,
            "headers": {"Content-Type": "application/json"},
            "body": json.dumps({"error": "Missing 'message' in request body"}),
        }

    agent = build_agent()
    reply = agent.generate_reply(messages=[{"role": "user", "content": message}])

    return {
        "statusCode": 200,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps({"reply": reply}),
    }
EOF

python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install autogen boto3 -t .
zip -r function.zip .
  1. Create the Lambda function and set the environment variable.
    Use the Python runtime that matches your local version, and give Lambda enough memory/time for model calls.
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-assistant \
  --runtime python3.11 \
  --handler app.lambda_handler \
  --zip-file fileb://function.zip \
  --role "$ROLE_ARN" \
  --timeout 30 \
  --memory-size 512 \
  --environment Variables="{OPENAI_API_KEY=$OPENAI_API_KEY}"
  1. Expose the function through an HTTP API so you can call it from curl or your app.
    This gives you a clean endpoint without having to wire up API Gateway’s older REST API flow.
API_ID=$(aws apigatewayv2 create-api \
  --name autogen-api \
  --protocol-type HTTP \
  --target arn:aws:lambda:$(aws configure get region):$(aws sts get-caller-identity --query Account --output text):function:autogen-assistant \
  --query ApiId --output text)

aws lambda add-permission \
  --function-name autogen-assistant \
  --statement-id apigw-invoke \
  --action lambda:InvokeFunction \
  --principal apigateway.amazonaws.com \
  --source-arn arn:aws:execute-api:$(aws configure get region):$(aws sts get-caller-identity --query Account --output text):$API_ID/*/*/

Testing It

Send a POST request with JSON containing a message field. If everything is wired correctly, you should get back a JSON response with the model’s reply.

curl -X POST https://YOUR_API_ID.execute-api.YOUR_REGION.amazonaws.com/ \
  -H 'Content-Type: application/json' \
  -d '{"message":"Give me three benefits of using S3 over EBS for static assets."}'

If you get a 500, check CloudWatch logs first. The usual failures are missing OPENAI_API_KEY, packaging issues from native dependencies, or an invalid AutoGen import version mismatch.

For production, keep an eye on Lambda timeout and memory settings. Model calls can be slow enough that the default timeout is too tight, especially if your prompt grows or you add tool use later.

Next Steps

  • Add structured input validation with Pydantic before calling AutoGen.
  • Move secrets from environment variables to AWS Secrets Manager.
  • Upgrade this single-agent setup to a multi-agent workflow with tool calling and explicit termination conditions.

Keep learning

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

Related Guides