Feat Request: Add support for uv based Python builds
Describe the solution you'd like.
The module now supports adding Python dependencies and requirements through pip and poetry. Is it possible to support uv as well? Note that uv also has good support for building AWS Lambda specific packages.
Describe alternatives you've considered.
As a workaround, I'm using these commands in source_path for building with uv:
source_path = [
{
path = "${path.module}/../../${var.source_dir_path}/src/"
commands = [
":zip . app",
]
patterns = [
"!.*/__pycache__.*",
"!.*/.*terragrunt.*",
]
},
{
path = "${path.module}/../../../${var.source_dir_path}"
commands = [
"uv export --frozen --no-dev --no-editable -o requirements.txt",
join(" ", [
"uv pip install",
"--no-installer-metadata",
"--no-compile-bytecode",
"--python-platform x86_64-manylinux2014",
"--python ${replace(var.runtime, "python", "")}",
"--target packages",
"-r requirements.txt"
]),
"cd packages",
":zip"
]
}
]
This works as intended and is a viable solution. However, it would be nice to have this as an officially supported feature, something like how poetry is supported.
Additional context
I created this feature request also for knowledge-sharing purposes, so other people can benefit from this implementation as a reference.
Thank you for already making this module so flexible that builds like this is possible.
Thank you for sharing this, I wasn't aware of that when I created my first issue - sadly locked as of now. I wish this one would gain some attention. (for reference https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/654#issuecomment-2791383267)
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
Still a valid feature request.
Still very much a viable request
I too would also like to see it implemented in the near future with proper documentation. Even building with Poetry currently lacks the proper documentation.
Hi @antonbabenko,
What about doing some changes to allow this:
module "my_lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "X.Y.Z"
function_name = "my-uv-lambda"
runtime = "python3.11"
handler = "app.handler"
source_path = "./lambda"
build_strategy = {
source = "terraform-aws-modules/lambda-build-uv/aws"
version = "1.0.0"
}
}
Then we could move the build implementation as a plugin as you suggested in #654 Then build_command will be picked either from build_command parameter or from build_strategy internal build_command.
Do you agree with that approach? Maybe I could create the PR. Thanks in advance
Still a valid feature request.
Thanks OP for the example!
I've adapted it a bit to work for Lambda layers:
module "lambda_layer" {
source = "terraform-aws-modules/lambda/aws"
version = "8.0.1"
create_layer = true
layer_name = "my-lambda"
runtime = "python${var.python_version}"
compatible_runtimes = ["python${var.python_version}"]
source_path = [{
commands = [
"rm -rf packages",
"uv export --frozen --no-dev --no-group test --no-editable --no-emit-project -o pylock.toml",
join(" ", [
"uv pip install",
"--no-installer-metadata",
"--no-compile-bytecode",
"--python-platform x86_64-manylinux2014",
"--python ${var.python_version}",
"--target packages/python",
"-r pylock.toml",
]),
"cd packages",
":zip",
]
path = "${path.module}/",
prefix_in_zip = "python",
patterns = [
"!(.*/)?src(/.*)?",
"!(.*/)?tests(/.*)?",
"!(.*/)?.venv(/.*)?",
"!(.*/)?.tox(/.*)?",
"!(.*/)?.pytest_cache(/.*)?",
]
}]
artifacts_dir = "${path.root}/.terraform/lambda-mylambda-layer-builds/"
recreate_missing_package = true
}
Note the rm -rf packages to avoid accumulating files from previous builds.
Also, I've switched from using requirements.txt to pylock.toml to work-around a limitation of the former that I was facing.
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
Still a valid request
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
Not stale.
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
Not stale.
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
#notstale