How to Integrate OpenAI for healthcare with AWS Lambda for startups

By Cyprian AaronsUpdated 2026-04-21
openai-for-healthcareaws-lambdastartups

Combining OpenAI for healthcare with AWS Lambda gives you a practical way to run healthcare workflows without standing up a full backend. You can take an incoming event, sanitize and route it through Lambda, call OpenAI for structured clinical reasoning or document extraction, then return a controlled response for an AI agent.

This pattern is useful when you need low-ops execution for things like patient intake triage, prior-auth document parsing, benefits Q&A, or summarizing clinical notes. Lambda handles the event-driven plumbing; OpenAI handles the language-heavy work.

Prerequisites

  • An AWS account with permission to create and invoke Lambda functions
  • AWS CLI configured locally: aws configure
  • Python 3.11+
  • boto3 installed for AWS access
  • openai Python SDK installed
  • An OpenAI API key stored in environment variables
  • A healthcare-compliant design in place:
    • only send the minimum necessary data
    • redact PHI where possible
    • log metadata, not raw medical text
  • IAM permissions for:
    • lambda:CreateFunction
    • lambda:InvokeFunction
    • iam:PassRole
  • A deployment path such as ZIP upload, SAM, or Serverless Framework

Install the dependencies:

pip install openai boto3

Integration Steps

1) Set up your environment variables

Keep credentials out of code. For startups, this is the fastest path to getting a safe baseline in place.

import os

os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["AWS_REGION"] = "us-east-1"
os.environ["LAMBDA_FUNCTION_NAME"] = "healthcare-agent-handler"

If you are deploying to Lambda, store these in Lambda environment variables instead of hardcoding them.


2) Build the Lambda handler that calls OpenAI

This function accepts an event payload, extracts the healthcare text, and sends it to OpenAI using the Chat Completions API. For production use, keep prompts narrow and ask for structured output.

import json
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def lambda_handler(event, context):
    patient_note = event.get("patient_note", "")
    if not patient_note:
        return {
            "statusCode": 400,
            "body": json.dumps({"error": "patient_note is required"})
        }

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "system",
                "content": (
                    "You are a healthcare assistant. "
                    "Return concise, structured output. "
                    "Do not invent facts."
                )
            },
            {
                "role": "user",
                "content": f"Summarize this note into assessment and next steps:\n\n{patient_note}"
            }
        ],
        temperature=0.2,
    )

    summary = response.choices[0].message.content

    return {
        "statusCode": 200,
        "body": json.dumps({
            "summary": summary
        })
    }

That is the core integration point: Lambda receives the event, OpenAI processes the text, Lambda returns JSON.


3) Deploy the function to AWS Lambda

Package your code and deploy it with a role that allows CloudWatch logging. Here is a minimal example using boto3.

import boto3
import zipfile
from pathlib import Path

lambda_client = boto3.client("lambda", region_name="us-east-1")

def package_code():
    zip_path = Path("/tmp/healthcare_agent.zip")
    with zipfile.ZipFile(zip_path, "w") as zf:
        zf.write("app.py")
    return str(zip_path)

zip_file = package_code()

with open(zip_file, "rb") as f:
    lambda_client.create_function(
        FunctionName="healthcare-agent-handler",
        Runtime="python3.11",
        Role="arn:aws:iam::123456789012:role/lambda-execution-role",
        Handler="app.lambda_handler",
        Code={"ZipFile": f.read()},
        Description="Healthcare agent integration with OpenAI",
        Timeout=30,
        MemorySize=512,
        Publish=True,
    )

If the function already exists, use update_function_code() instead of create_function().


4) Invoke Lambda from your agent service or orchestration layer

In a startup setup, your API service or agent orchestrator should call Lambda asynchronously or synchronously depending on latency needs. This example uses direct invocation.

import json
import boto3

lambda_client = boto3.client("lambda", region_name="us-east-1")

payload = {
    "patient_note": (
        "Patient reports fever for 2 days and mild cough. "
        "No shortness of breath. Wants advice on next steps."
    )
}

response = lambda_client.invoke(
    FunctionName="healthcare-agent-handler",
    InvocationType="RequestResponse",
    Payload=json.dumps(payload).encode("utf-8"),
)

result = json.loads(response["Payload"].read())
print(result)

For agent systems, this pattern keeps your orchestration layer thin while pushing the language task into serverless compute.


5) Add basic safety checks before sending data to OpenAI

Healthcare workloads need guardrails. Strip obvious identifiers before sending payloads downstream.

import re

def redact_phi(text: str) -> str:
    text = re.sub(r"\b\d{3}-\d{2}-\d{4}\b", "[REDACTED_SSN]", text)
    text = re.sub(r"\b\d{10}\b", "[REDACTED_PHONE]", text)
    text = re.sub(r"\b[\w\.-]+@[\w\.-]+\.\w+\b", "[REDACTED_EMAIL]", text)
    return text

clean_note = redact_phi(payload["patient_note"])

Use this before calling client.chat.completions.create(...). In production, you would also add policy checks, audit logs, and allowlists for fields that can leave your system.

Testing the Integration

Run a local test against your handler before wiring it into an agent flow.

if __name__ == "__main__":
    test_event = {
        "patient_note": (
            "Patient has sore throat and fever. Symptoms started yesterday. "
            "No allergy history reported."
        )
    }

    output = lambda_handler(test_event, None)
    print(output)

Expected output:

{
  "statusCode": 200,
  "body": "{\"summary\": \"...structured assessment and next steps...\"}"
}

If you want to test end-to-end through AWS Lambda:

import boto3
import json

client = boto3.client("lambda", region_name="us-east-1")

resp = client.invoke(
    FunctionName="healthcare-agent-handler",
    InvocationType="RequestResponse",
    Payload=json.dumps({
        "patient_note": "Patient reports headache and nausea after starting new medication."
    }).encode("utf-8")
)

print(json.loads(resp["Payload"].read()))

Real-World Use Cases

  • Patient intake triage

    • Convert free-text intake notes into structured summaries for care coordinators.
    • Route urgent cases to a human reviewer based on model output plus rules.
  • Prior authorization document processing

    • Extract diagnoses, medications, and treatment history from uploaded notes.
    • Generate structured JSON that downstream systems can validate.
  • Clinical inbox assistant

    • Summarize incoming patient messages.
    • Draft responses for staff review before anything goes back to the patient.

This stack works well for startups because it is simple to operate and easy to extend. Lambda gives you event-driven execution; OpenAI gives you language understanding; your job is to keep the interface narrow, validated, and auditable.


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