HeadlinesBriefing favicon HeadlinesBriefing.com

uv Workspaces for Python Monorepos and AWS Lambda

DEV Community •
×

uv workspaces streamline Python monorepo development by creating a single virtual environment for interconnected packages. Running `uv init` in a subfolder automatically sets this up when a root `pyproject.toml` exists. This approach eliminates constant context switching between venvs for different services, though it introduces dependency conflicts if projects aren't version-compatible.

The real challenge emerges when building AWS Lambda container images. Docker builds must avoid installing all workspace dependencies, as some are microservice-specific. More critically, core dependencies and local packages shouldn't share layers, since core libraries change infrequently but are larger. Lambda cold starts are directly impacted by image size and layer caching efficiency.

A refined Docker strategy uses `uv sync` with `--no-install-local` for core dependencies, then binds local packages separately before copying source code. This prevents duplicate files but leaves `uv` artifacts in the final image. A cleaner two-stage build uses `uv export` to generate requirements, installing them with `pip` into dedicated folders. The final image contains only runtime essentials: core libraries, common dependencies, and application code.