Module 6 Lesson 1: FastAPI + Bedrock
Creating the Backend. How to wrap your Bedrock logic in a high-performance REST API.
Serving Your AI: The FastAPI Bridge
A Python script in a terminal is a prototype. A FastAPI endpoint is a Product. To build a website or mobile app, you must serve your Bedrock code over the web.
1. Why FastAPI?
FastAPI is built for Asynchronous code. Since calling an LLM takes time (1-10 seconds), async allows your server to handle other users while it's waiting for Bedrock to finish.
2. Basic API Implementation
from fastapi import FastAPI
from pydantic import BaseModel
import boto3
app = FastAPI()
client = boto3.client("bedrock-runtime", region_name="us-east-1")
class ChatRequest(BaseModel):
user_prompt: str
@app.post("/chat")
async def chat(request: ChatRequest):
messages = [{"role": "user", "content": [{"text": request.user_prompt}]}]
response = client.converse(
modelId="anthropic.claude-3-haiku-20240307-v1:0",
messages=messages
)
reply = response["output"]["message"]["content"][0]["text"]
return {"reply": reply}
3. Visualizing the Web Request
graph LR
U[Mobile App] -->|POST /chat| F[FastAPI]
F -->|Invoke| B[AWS Bedrock]
B -->|JSON| F
F -->|JSON: {reply...}| U
4. Input Validation
Never trust the user. Use Pydantic (the ChatRequest class above) to ensure they send a string. You can also add limits:
class ChatRequest(BaseModel):
user_prompt: str = Field(..., max_length=500, min_length=2)
This prevents malicious users from sending a 50-page prompt that costs you a fortune.
Summary
- FastAPI is the preferred backend for Bedrock.
- Asynchronous handling prevents blocking other requests.
- Pydantic is mandatory for securing your API from input abuse.
- Wrap your logic in POST endpoints to handle complex prompt data.