LangChain Tutorial (Python): deploying to AWS Lambda for advanced developers
This tutorial shows how to package a LangChain-based Python app for AWS Lambda, expose it through API Gateway, and keep the deployment small enough to stay within Lambda limits. You need this when you want a serverless AI endpoint that can answer requests without running a container or managing EC2.
What You'll Need
- •Python 3.11
- •AWS account with permissions for:
- •Lambda
- •API Gateway
- •IAM role creation
- •CloudWatch Logs
- •AWS CLI configured locally
- •An OpenAI API key set as an environment variable
- •These Python packages:
- •
langchain - •
langchain-openai - •
boto3 - •
pydantic
- •
- •A deployment zip built on Linux-compatible binaries, or inside Docker if you’re on macOS/Windows
- •Basic familiarity with Lambda handlers and API Gateway proxy events
Step-by-Step
- •Create a minimal LangChain app that can run inside Lambda.
Keep the chain simple: accept JSON input, call the model, return plain text. Lambda cold starts punish oversized dependency trees, so avoid extra abstractions.
import json
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise assistant for insurance operations."),
("user", "{question}")
])
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0,
api_key=os.environ["OPENAI_API_KEY"],
)
chain = prompt | llm
def lambda_handler(event, context):
body = json.loads(event.get("body") or "{}")
question = body.get("question", "What is an exclusion clause?")
result = chain.invoke({"question": question})
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"answer": result.content}),
}
- •Package dependencies into a Lambda-friendly build directory.
Install everything into a local folder that you zip and upload. If you’re building on macOS or Windows, use a Linux Docker image so compiled dependencies match Lambda’s runtime.
mkdir -p build
python3.11 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install \
langchain \
langchain-openai \
pydantic \
boto3 \
-t build
cp app.py build/
cd build && zip -r ../lambda-langchain.zip . && cd ..
- •Create the IAM role and Lambda function.
The function only needs basic execution permissions for CloudWatch logs. Set the OpenAI key as an environment variable so your code stays free of secrets.
aws iam create-role \
--role-name langchain-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 langchain-lambda-role \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
ROLE_ARN=$(aws iam get-role --role-name langchain-lambda-role --query 'Role.Arn' --output text)
aws lambda create-function \
--function-name langchain-python-api \
--runtime python3.11 \
--handler app.lambda_handler \
--zip-file fileb://lambda-langchain.zip \
--role "$ROLE_ARN" \
--timeout 30 \
--memory-size 512 \
--environment Variables="{OPENAI_API_KEY=$OPENAI_API_KEY}"
- •Add API Gateway so external callers can hit the function over HTTPS.
For advanced use cases, HTTP API is usually enough and cheaper than REST API. You’ll map the route directly to the Lambda integration.
API_ID=$(aws apigatewayv2 create-api \
--name langchain-http-api \
--protocol-type HTTP \
--query 'ApiId' --output text)
INTEGRATION_ID=$(aws apigatewayv2 create-integration \
--api-id "$API_ID" \
--integration-type AWS_PROXY \
--integration-uri "$(aws lambda get-function --function-name langchain-python-api --query 'Configuration.FunctionArn' --output text)" \
--payload-format-version "2.0" \
--query 'IntegrationId' --output text)
aws apigatewayv2 create-route \
--api-id "$API_ID" \
--route-key 'POST /ask' \
--target "integrations/$INTEGRATION_ID"
aws apigatewayv2 create-stage \
--api-id "$API_ID" \
--stage-name prod \
--auto-deploy
- •Grant API Gateway permission to invoke the function and test it end-to-end.
This is the part people skip and then wonder why they get403or502. Once permission is in place, send a POST request with a JSON body containing your prompt.
aws lambda add-permission \
--function-name langchain-python-api \
--statement-id apigw-invoke-permission \
--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/*/*/ask"
URL="https://${API_ID}.execute-api.$(aws configure get region).amazonaws.com/prod/ask"
curl -s "$URL" \
-H "Content-Type: application/json" \
-d '{"question":"Write one sentence explaining claims reserving."}'
Testing It
Start by checking CloudWatch Logs for the Lambda invocation if the request fails; most issues will be missing environment variables, bad packaging, or an invalid handler path. If you see timeouts, increase memory first because Lambda CPU scales with memory.
Then verify that the response body contains valid JSON with an answer field and that the model output matches your prompt style. If you’re using OpenAI models in production, also test rate limiting and retry behavior under load.
For a quick sanity check, invoke the function directly from AWS before going through API Gateway:
aws lambda invoke \
--function-name langchain-python-api \
--payload '{"body":"{\"question\":\"What does subrogation mean?\"}"}' \
response.json
cat response.json
Next Steps
- •Move secrets from Lambda environment variables to AWS Secrets Manager.
- •Add structured output with Pydantic models so downstream systems can parse responses reliably.
- •Introduce tracing with LangSmith so you can inspect prompts, latency, and token usage in production paths.
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