From e7ef06458059e9c558ff05f30035e80e5199d75f Mon Sep 17 00:00:00 2001 From: moti-254 Date: Thu, 28 May 2026 19:02:45 +0300 Subject: [PATCH] Fix AWS Lambda container compatibility --- week1/day5.md | 53 ++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/week1/day5.md b/week1/day5.md index bdf812a..850957e 100644 --- a/week1/day5.md +++ b/week1/day5.md @@ -381,64 +381,69 @@ Docker lets us package everything into a single container that runs anywhere. Create `Dockerfile` in your project root: ```dockerfile -# Stage 1: Build the Next.js static files -FROM node:22-alpine AS frontend-builder + +# ========================= +# Stage 1: Build Next.js frontend +# ========================= +FROM --platform=linux/amd64 node:22-alpine AS frontend-builder WORKDIR /app -# Copy package files first (for better caching) +# Copy package files first for caching COPY package*.json ./ + +# Install dependencies RUN npm ci -# Copy all frontend files +# Copy project files COPY . . -# Build argument for Clerk public key +# Clerk public key ARG NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY ENV NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=$NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY -# Note: Docker may warn about "secrets in ARG/ENV" - this is OK! -# The NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is meant to be public (it starts with pk_) - -# Build the Next.js app (creates 'out' directory with static files) +# Build Next.js static export RUN npm run build -# Stage 2: Create the final Python container -FROM python:3.12-slim + +# ========================= +# Stage 2: Python + FastAPI + Lambda +# ========================= +FROM --platform=linux/amd64 python:3.12-slim WORKDIR /app -# --- Lambda Web Adapter additions (the only changes vs. the video's Dockerfile) --- -# Drops a Lambda extension binary into /opt/extensions. The binary is inert -# unless invoked by the Lambda runtime, so local `docker run` is unaffected. -COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:1.0.0 /lambda-adapter /opt/extensions/lambda-adapter +# Install Lambda Web Adapter +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.1 \ + /lambda-adapter \ + /opt/extensions/lambda-adapter -# Tell the adapter which port FastAPI listens on +# Environment variables ENV PORT=8000 - -# Enable Lambda response streaming (required so SSE / streaming endpoints work end-to-end) ENV AWS_LWA_INVOKE_MODE=response_stream -# --- end of Lambda Web Adapter additions --- # Install Python dependencies COPY requirements.txt . + RUN pip install --no-cache-dir -r requirements.txt -# Copy the FastAPI server +# Copy FastAPI server COPY api/server.py . -# Copy the Next.js static export from builder stage +# Copy Next.js static files COPY --from=frontend-builder /app/out ./static -# Health check (used during local Docker testing; Lambda does not call it) +# Healthcheck HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" -# Expose port 8000 (FastAPI will serve everything) +# Expose FastAPI port EXPOSE 8000 -# Start the FastAPI server +# Start FastAPI server CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"] + + ``` ### Step 2: Create .dockerignore