|
| 1 | +# FastAPI CI/CD Example |
| 2 | + |
| 3 | +A FastAPI application example with functional tests that run both locally and in CI/CD pipelines. This example demonstrates how to test Lambda Web Adapter applications using both Docker and SAM local. |
| 4 | + |
| 5 | +See the full working repository with CI/CD: [andyreagan/aws-lamdba-web-adapter-functional-testing](https://github.com/andyreagan/aws-lamdba-web-adapter-functional-testing) |
| 6 | + |
| 7 | +## Key Features |
| 8 | + |
| 9 | +- **Docker testing**: Validates the app works as a standalone web server |
| 10 | +- **SAM local testing**: Validates the app works inside the Lambda runtime with the Web Adapter |
| 11 | +- **CI/CD ready**: GitHub Actions workflow included (see `.github/workflows/test.yml`) |
| 12 | +- **Modern Python tooling**: Uses `uv` for fast, reproducible dependency management |
| 13 | + |
| 14 | +## Application Structure |
| 15 | + |
| 16 | +``` |
| 17 | +fastapi-cicd/ |
| 18 | +├── app/ |
| 19 | +│ ├── __init__.py |
| 20 | +│ └── main.py # FastAPI application |
| 21 | +├── tests/ |
| 22 | +│ ├── conftest.py # Test fixtures for server management |
| 23 | +│ └── test_functional.py |
| 24 | +├── .github/ |
| 25 | +│ └── workflows/ |
| 26 | +│ └── test.yml # CI/CD pipeline |
| 27 | +├── Dockerfile |
| 28 | +├── template.yaml # SAM template |
| 29 | +├── pyproject.toml |
| 30 | +└── uv.lock |
| 31 | +``` |
| 32 | + |
| 33 | +## Dockerfile |
| 34 | + |
| 35 | +```dockerfile |
| 36 | +FROM public.ecr.aws/docker/library/python:3.12-slim AS base |
| 37 | + |
| 38 | +# Install Lambda Web Adapter |
| 39 | +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.1 /lambda-adapter /opt/extensions/lambda-adapter |
| 40 | + |
| 41 | +# Install uv |
| 42 | +COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv |
| 43 | + |
| 44 | +WORKDIR /var/task |
| 45 | +COPY pyproject.toml uv.lock ./ |
| 46 | +COPY app/ ./app/ |
| 47 | +RUN uv sync --frozen --no-dev |
| 48 | + |
| 49 | +ENV PORT=8000 |
| 50 | +EXPOSE 8000 |
| 51 | +CMD ["uv", "run", "uvicorn", "--host", "0.0.0.0", "--port", "8000", "app.main:app"] |
| 52 | +``` |
| 53 | + |
| 54 | +## Pre-requisites |
| 55 | + |
| 56 | +- [AWS CLI](https://aws.amazon.com/cli/) |
| 57 | +- [SAM CLI](https://github.com/awslabs/aws-sam-cli) |
| 58 | +- [Python 3.12+](https://www.python.org/) |
| 59 | +- [Docker](https://www.docker.com/products/docker-desktop) |
| 60 | +- [uv](https://github.com/astral-sh/uv) |
| 61 | + |
| 62 | +## Setup |
| 63 | + |
| 64 | +```bash |
| 65 | +uv sync |
| 66 | +``` |
| 67 | + |
| 68 | +## Running Tests Locally |
| 69 | + |
| 70 | +```bash |
| 71 | +# Build both Docker image and SAM application |
| 72 | +docker build -t hello-world-lambda . |
| 73 | +sam build |
| 74 | + |
| 75 | +# Run all functional tests |
| 76 | +uv run pytest tests/test_functional.py -v |
| 77 | +``` |
| 78 | + |
| 79 | +The tests spin up: |
| 80 | +1. **Docker container directly** - validates the app works as a web server |
| 81 | +2. **SAM local** - validates the app works inside the Lambda runtime with the Web Adapter |
| 82 | + |
| 83 | +## Deploy to Lambda |
| 84 | + |
| 85 | +Build and deploy using SAM: |
| 86 | + |
| 87 | +```bash |
| 88 | +sam build |
| 89 | +sam deploy --guided |
| 90 | +``` |
| 91 | + |
| 92 | +## Run Docker Locally |
| 93 | + |
| 94 | +Run the same Docker image locally (portable to ECS/EKS): |
| 95 | + |
| 96 | +```bash |
| 97 | +docker run -d -p 8000:8000 hello-world-lambda |
| 98 | +curl localhost:8000/ |
| 99 | +curl localhost:8000/health |
| 100 | +``` |
0 commit comments