Skip to content

Commit 88d8221

Browse files
committed
feat: add uv support for python packaging (fixes #673)
1 parent 177ee12 commit 88d8221

File tree

8 files changed

+388
-2
lines changed

8 files changed

+388
-2
lines changed

examples/build-package/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Configuration in this directory creates deployment packages in a variety of comb
55
This example demonstrates various packaging scenarios including:
66
- Python packages with pip requirements
77
- Poetry-based Python packages
8+
- UV-based Python packages
89
- Node.js packages with npm
910
- Docker-based builds
1011
- Quiet packaging - suppressing Poetry/pip/npm output during builds using `quiet_archive_local_exec = true`
@@ -46,12 +47,15 @@ Note that this example may create resources which cost money. Run `terraform des
4647
| <a name="module_lambda_layer"></a> [lambda\_layer](#module\_lambda\_layer) | ../../ | n/a |
4748
| <a name="module_lambda_layer_pip_requirements"></a> [lambda\_layer\_pip\_requirements](#module\_lambda\_layer\_pip\_requirements) | ../.. | n/a |
4849
| <a name="module_lambda_layer_poetry"></a> [lambda\_layer\_poetry](#module\_lambda\_layer\_poetry) | ../../ | n/a |
50+
| <a name="module_lambda_layer_uv"></a> [lambda\_layer\_uv](#module\_lambda\_layer\_uv) | ../../ | n/a |
4951
| <a name="module_npm_package_with_commands_and_patterns"></a> [npm\_package\_with\_commands\_and\_patterns](#module\_npm\_package\_with\_commands\_and\_patterns) | ../../ | n/a |
5052
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
5153
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
5254
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |
5355
| <a name="module_package_dir_poetry_no_docker"></a> [package\_dir\_poetry\_no\_docker](#module\_package\_dir\_poetry\_no\_docker) | ../../ | n/a |
5456
| <a name="module_package_dir_poetry_quiet"></a> [package\_dir\_poetry\_quiet](#module\_package\_dir\_poetry\_quiet) | ../../ | n/a |
57+
| <a name="module_package_dir_uv"></a> [package\_dir\_uv](#module\_package\_dir\_uv) | ../../ | n/a |
58+
| <a name="module_package_dir_uv_no_docker"></a> [package\_dir\_uv\_no\_docker](#module\_package\_dir\_uv\_no\_docker) | ../../ | n/a |
5559
| <a name="module_package_dir_with_npm_install"></a> [package\_dir\_with\_npm\_install](#module\_package\_dir\_with\_npm\_install) | ../../ | n/a |
5660
| <a name="module_package_dir_with_npm_install_lock_file"></a> [package\_dir\_with\_npm\_install\_lock\_file](#module\_package\_dir\_with\_npm\_install\_lock\_file) | ../../ | n/a |
5761
| <a name="module_package_dir_without_npm_install"></a> [package\_dir\_without\_npm\_install](#module\_package\_dir\_without\_npm\_install) | ../../ | n/a |

examples/build-package/main.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,67 @@ module "package_dir_poetry_quiet" {
137137
quiet_archive_local_exec = true # Suppress Poetry/pip output during packaging
138138
}
139139

140+
# Create zip-archive of a single directory where "uv export" & "pip install" will be executed (using docker)
141+
module "package_dir_uv" {
142+
source = "../../"
143+
144+
create_function = false
145+
146+
build_in_docker = true
147+
runtime = "python3.12"
148+
docker_image = "build-python-uv"
149+
docker_file = "${path.module}/../fixtures/python-app-uv/docker/Dockerfile"
150+
151+
source_path = [
152+
{
153+
path = "${path.module}/../fixtures/python-app-uv"
154+
uv_install = true
155+
}
156+
]
157+
artifacts_dir = "${path.root}/builds/package_dir_uv/"
158+
}
159+
160+
# Create zip-archive of a single directory where "uv export" & "pip install" will be executed (not using docker)
161+
module "package_dir_uv_no_docker" {
162+
source = "../../"
163+
164+
create_function = false
165+
166+
runtime = "python3.12"
167+
168+
source_path = [
169+
{
170+
path = "${path.module}/../fixtures/python-app-uv"
171+
uv_install = true
172+
}
173+
]
174+
artifacts_dir = "${path.root}/builds/package_dir_uv_no_docker/"
175+
}
176+
177+
# Create a Lambda Layer using uv for dependency management (using docker)
178+
module "lambda_layer_uv" {
179+
source = "../../"
180+
181+
create_layer = true
182+
layer_name = "${random_pet.this.id}-layer-uv"
183+
compatible_runtimes = ["python3.12"]
184+
runtime = "python3.12"
185+
186+
build_in_docker = true
187+
docker_image = "build-python-uv"
188+
docker_file = "${path.module}/../fixtures/python-app-uv/docker/Dockerfile"
189+
190+
source_path = [
191+
{
192+
path = "${path.module}/../fixtures/python-app-uv"
193+
uv_install = true
194+
prefix_in_zip = "python"
195+
}
196+
]
197+
hash_extra = "uv-extra-hash-to-prevent-conflicts-with-module.package_dir"
198+
artifacts_dir = "${path.root}/builds/lambda_layer_uv/"
199+
}
200+
140201
# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
141202
module "package_dir_without_pip_install" {
142203
source = "../../"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM public.ecr.aws/sam/build-python3.12
2+
3+
LABEL description="AWS Lambda build container with uv"
4+
5+
RUN pip install uv==0.9.21
6+
7+
WORKDIR /var/task
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def lambda_handler(event, context):
2+
import requests
3+
4+
print("Hello from uv-managed Lambda!")
5+
return {"statusCode": 200}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
name = "uv-lambda"
3+
version = "0.1.0"
4+
requires-python = ">=3.12"
5+
dependencies = [
6+
"requests==2.31.0",
7+
]
8+
9+
[tool.uv]
10+
# This marker is for our Terraform parser to detect UV
11+
12+
[build-system]
13+
requires = ["hatchling"]
14+
build-backend = "hatchling.build"

examples/fixtures/python-app-uv/uv.lock

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)