|
1 | 1 | # uv-python-lambda |
2 | 2 |
|
3 | | -CDK Construct for Python Lambda Functions using [uv](https://docs.astral.sh/uv/) |
| 3 | +CDK construct for packaging Python Lambda functions from `uv` projects. |
4 | 4 |
|
5 | | -## Goals |
| 5 | +## Why use it |
6 | 6 |
|
7 | | -- ⚡️ Package and deploy Lambda Functions faster with `uv`'s speed |
8 | | -- 📦 Support workspaces in a monorepo with [uv workspaces](https://docs.astral.sh/uv/concepts/workspaces/) |
| 7 | +- Packages Lambda assets from a `uv` project. |
| 8 | +- Supports `uv` workspaces, so you can package one workspace package while resolving dependencies from the workspace root. |
| 9 | +- Keeps the usual CDK Lambda experience: `PythonFunction` extends `aws_lambda.Function`, derives the handler from `index` and `handler` |
| 10 | +- Reuses builder containers across compatible functions during synthesis to reduce repeated setup work. |
9 | 11 |
|
10 | | -`uv-python-lambda` is based on [aws-lambda-python-alpha](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-lambda-python-alpha-readme.html) with some differences: |
| 12 | +☝️ NOTE: This construct defaults to Python 3.12 on **ARM64**. |
11 | 13 |
|
12 | | -- It only supports `uv` for packaging - there is no Poetry or pip support |
13 | | -- It supports workspaces so you can build multiple Lambda functions from different uv workspaces and have their dependencies included correctly. This is useful for, but not limited to, monorepos. |
| 14 | +This library is published for TypeScript/JavaScript and Python. |
14 | 15 |
|
15 | | -## API |
| 16 | +## Install |
16 | 17 |
|
17 | | -See [API.md](API.md) |
| 18 | +TypeScript / JavaScript: |
18 | 19 |
|
19 | | -## Example |
| 20 | +```bash |
| 21 | +npm install uv-python-lambda aws-cdk-lib constructs |
| 22 | +``` |
| 23 | + |
| 24 | +Python: |
| 25 | + |
| 26 | +```bash |
| 27 | +uv add uv-python-lambda aws-cdk-lib constructs |
| 28 | +``` |
| 29 | + |
| 30 | +Docker is required for bundling. |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +TypeScript: |
| 35 | + |
| 36 | +```ts |
| 37 | +import * as path from 'node:path'; |
| 38 | +import { Stack, Duration } from 'aws-cdk-lib'; |
| 39 | +import { PythonFunction } from 'uv-python-lambda'; |
| 40 | +import type { Construct } from 'constructs'; |
| 41 | + |
| 42 | +export class ExampleStack extends Stack { |
| 43 | + constructor(scope: Construct, id: string) { |
| 44 | + super(scope, id); |
| 45 | + |
| 46 | + new PythonFunction(this, 'Fn', { |
| 47 | + rootDir: path.join(__dirname, '..', '..', 'services', 'fetcher'), |
| 48 | + index: 'handler.py', |
| 49 | + handler: 'lambda_handler', |
| 50 | + timeout: Duration.seconds(30), |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Python: |
20 | 57 |
|
21 | 58 | ```python |
22 | | -from uv_python_lambda import PythonFunction |
23 | | -from constructs import Construct |
| 59 | +from pathlib import Path |
24 | 60 |
|
25 | | -# The root path should be relative to your CDK source file |
26 | | -root_path = Path(__file__).parent.parent.parent |
| 61 | +from aws_cdk import Duration, Stack |
| 62 | +from constructs import Construct |
| 63 | +from uv_python_lambda import PythonFunction |
27 | 64 |
|
28 | 65 |
|
29 | | -class CdkStack(Stack): |
| 66 | +class ExampleStack(Stack): |
30 | 67 | def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: |
31 | 68 | super().__init__(scope, construct_id, **kwargs) |
32 | 69 |
|
33 | | - fn = PythonFunction( |
34 | | - self, |
35 | | - "fn", |
36 | | - root_dir=str(root_path), |
37 | | - index="fetcher_lambda.py", |
38 | | - workspace_package="fetcher", # Use a workspace package as the top-level Lambda entry point. |
39 | | - handler="handle_event", |
40 | | - bundling={ |
41 | | - "asset_excludes": [ |
42 | | - ".venv/", |
43 | | - "node_modules/", |
44 | | - "cdk/", |
45 | | - ".git/", |
46 | | - ".idea/", |
47 | | - "dist/", |
48 | | - ] |
49 | | - }, |
50 | | - timeout=Duration.seconds(30), |
| 70 | + root_dir = Path(__file__).resolve().parents[2] / "services" / "fetcher" |
| 71 | + |
| 72 | + PythonFunction( |
| 73 | + self, |
| 74 | + "Fn", |
| 75 | + root_dir=str(root_dir), |
| 76 | + index="handler.py", |
| 77 | + handler="lambda_handler", |
| 78 | + timeout=Duration.seconds(30), |
51 | 79 | ) |
52 | | -``` |
| 80 | +``` |
| 81 | + |
| 82 | +## Using workspaces |
| 83 | + |
| 84 | +Point `rootDir` or `root_dir` at the workspace root, then set `workspacePackage` or `workspace_package` to the package that contains the Lambda entrypoint. |
| 85 | + |
| 86 | +```ts |
| 87 | +new PythonFunction(this, 'WorkspaceFn', { |
| 88 | + rootDir: path.join(__dirname, '..', '..'), |
| 89 | + workspacePackage: 'fetcher', |
| 90 | + index: 'fetcher_lambda.py', |
| 91 | + handler: 'handle_event', |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +```python |
| 96 | +PythonFunction( |
| 97 | + self, |
| 98 | + "WorkspaceFn", |
| 99 | + root_dir=str(root_dir), |
| 100 | + workspace_package="fetcher", |
| 101 | + index="fetcher_lambda.py", |
| 102 | + handler="handle_event", |
| 103 | +) |
| 104 | +``` |
| 105 | + |
| 106 | +## Notes |
| 107 | + |
| 108 | +- `index` can be passed as `handler.py` or `handler`. |
| 109 | +- Use `bundling` to pass Docker environment variables, asset excludes, build args, or command hooks. |
| 110 | +- See [API.md](API.md) for the full API reference. |
0 commit comments