How to Integrate OpenAI for investment banking with AWS Lambda for RAG
Combining OpenAI with AWS Lambda gives you a clean way to run retrieval-augmented generation (RAG) for investment banking workloads without keeping servers warm. The pattern is simple: Lambda fetches and prepares deal docs, filings, or internal research from storage, then OpenAI turns that context into banker-grade answers, summaries, and draft outputs.
Prerequisites
- •Python 3.11 installed locally
- •AWS account with:
- •Lambda enabled
- •IAM role for Lambda execution
- •S3 bucket for document storage
- •CloudWatch Logs access
- •AWS CLI configured:
- •
aws configure
- •
- •OpenAI API key stored in AWS Secrets Manager or as an environment variable for local testing
- •Python packages:
- •
boto3 - •
openai - •
pydanticif you want structured outputs
- •
- •A source of RAG documents:
- •earnings transcripts
- •CIMs
- •pitch books
- •SEC filings
- •internal notes
Integration Steps
- •Create a Lambda handler that accepts a user query and retrieves relevant context
For investment banking RAG, keep the retrieval layer outside the model call. In this example, the Lambda function loads a document from S3 and uses it as context.
import json
import os
import boto3
s3 = boto3.client("s3")
BUCKET_NAME = os.environ["DOCS_BUCKET"]
def lambda_handler(event, context):
query = event.get("query", "")
doc_key = event.get("doc_key", "research/2024-q2-summary.txt")
obj = s3.get_object(Bucket=BUCKET_NAME, Key=doc_key)
document_text = obj["Body"].read().decode("utf-8")
return {
"statusCode": 200,
"body": json.dumps({
"query": query,
"context": document_text[:12000]
})
}
This is the basic RAG boundary. Lambda handles retrieval; OpenAI handles reasoning and response generation.
- •Call OpenAI from inside Lambda using the Responses API
Use the OpenAI Python SDK inside the function. For investment banking use cases, ask for concise output with explicit assumptions and risk flags.
import json
import os
import boto3
from openai import OpenAI
s3 = boto3.client("s3")
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
BUCKET_NAME = os.environ["DOCS_BUCKET"]
def lambda_handler(event, context):
query = event.get("query", "")
doc_key = event.get("doc_key", "research/2024-q2-summary.txt")
obj = s3.get_object(Bucket=BUCKET_NAME, Key=doc_key)
document_text = obj["Body"].read().decode("utf-8")
prompt = f"""
You are an investment banking analyst assistant.
Answer using only the provided context.
If data is missing, say so clearly.
Question:
{query}
Context:
{document_text}
"""
response = client.responses.create(
model="gpt-4.1-mini",
input=prompt,
temperature=0.2,
)
return {
"statusCode": 200,
"body": json.dumps({
"answer": response.output_text
})
}
The important part is client.responses.create(...). That is the actual SDK call you want in production for direct text generation.
- •Package the function and deploy it to AWS Lambda
Keep dependencies small. Bundle your handler with the OpenAI SDK and any parsing logic you need.
# requirements.txt
boto3==1.34.162
openai==1.40.6
A minimal deployment workflow looks like this:
mkdir package && cd package
pip install -r requirements.txt -t .
cp ../lambda_function.py .
zip -r function.zip .
aws lambda create-function \
--function-name ib-rag-assistant \
--runtime python3.11 \
--role arn:aws:iam::123456789012:role/lambda-exec-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zip \
--timeout 30 \
--memory-size 512 \
--environment Variables="{DOCS_BUCKET=ib-rag-docs,OPENAI_API_KEY=sk-...}"
In production, move OPENAI_API_KEY into Secrets Manager and inject it at runtime.
- •Add structured output so downstream systems can consume the result
Banking workflows usually need more than free-form text. You often want a summary, key risks, comps references, or action items in JSON.
import json
import os
import boto3
from openai import OpenAI
s3 = boto3.client("s3")
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def lambda_handler(event, context):
obj = s3.get_object(
Bucket=os.environ["DOCS_BUCKET"],
Key=event["doc_key"]
)
context_text = obj["Body"].read().decode("utf-8")
prompt = f"""
Return JSON with keys:
summary, key_risks, valuation_notes, follow_up_questions
Question: {event['query']}
Context: {context_text}
"""
response = client.responses.create(
model="gpt-4.1-mini",
input=prompt,
temperature=0,
)
return {
"statusCode": 200,
"body": json.dumps({
"result": response.output_text
})
}
If you need stricter schema enforcement, wrap this with Pydantic validation after parsing the model output.
- •Wire Lambda behind an API Gateway or invoke it directly from another agent
For agent systems, direct invocation is often cleaner than HTTP if another service already owns orchestration.
import json
import boto3
lambda_client = boto3.client("lambda")
payload = {
"query": "Summarize revenue growth drivers and highlight risks.",
"doc_key": "research/acme_q2.txt"
}
response = lambda_client.invoke(
FunctionName="ib-rag-assistant",
InvocationType="RequestResponse",
Payload=json.dumps(payload).encode("utf-8")
)
result = json.loads(response["Payload"].read())
print(result["body"])
The actual AWS SDK method here is lambda_client.invoke(...). This is useful when one agent calls another without exposing an external endpoint.
Testing the Integration
Use a real invocation test before wiring this into an agent loop.
import json
import boto3
lambda_client = boto3.client("lambda")
test_event = {
"query": "What are the main EBITDA margin pressures mentioned in this report?",
"doc_key": "research/acme_q2.txt"
}
response = lambda_client.invoke(
FunctionName="ib-rag-assistant",
InvocationType="RequestResponse",
Payload=json.dumps(test_event).encode("utf-8")
)
payload = json.loads(response["Payload"].read())
print(payload["body"])
Expected output:
{
"answer": "EBITDA margin pressure is driven by higher SG&A spend, elevated freight costs, and temporary integration expenses..."
}
If you get an empty answer or malformed JSON:
- •confirm the S3 object key exists
- •check Lambda logs in CloudWatch
- •verify
OPENAI_API_KEYis present in the runtime environment - •make sure your prompt limits the model to the supplied context
Real-World Use Cases
- •
Deal team Q&A assistant
- •Ask questions over CIMs, management presentations, and earnings transcripts.
- •Return concise answers with risk flags for bankers during live deal work.
- •
Investment memo drafting
- •Generate first-pass memos from internal research and filing extracts.
- •Use Lambda to pull source docs and OpenAI to draft sections like thesis, risks, and catalysts.
- •
Comparable company summarization
- •Retrieve comps tables from S3 or a data warehouse.
- •Have OpenAI produce a readable summary of valuation deltas and outliers for IC materials.
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