terraform-aws-lambda icon indicating copy to clipboard operation
terraform-aws-lambda copied to clipboard

Feat Request: Add support for uv based Python builds

Open jussapaavo opened this issue 9 months ago β€’ 18 comments

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.

jussapaavo avatar Apr 09 '25 10:04 jussapaavo

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)

mazzma12 avatar Apr 10 '25 07:04 mazzma12

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

github-actions[bot] avatar May 11 '25 00:05 github-actions[bot]

Still a valid feature request.

pawelpesz avatar May 12 '25 07:05 pawelpesz

Still very much a viable request

catrielg avatar May 19 '25 06:05 catrielg

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.

leirtac12 avatar May 19 '25 06:05 leirtac12

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

dmunicio avatar May 21 '25 08:05 dmunicio

Still a valid feature request.

dmunicio avatar Jun 09 '25 13:06 dmunicio

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.

pdecat avatar Jul 02 '25 09:07 pdecat

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

github-actions[bot] avatar Aug 02 '25 00:08 github-actions[bot]

Still a valid request

dmunicio avatar Aug 02 '25 09:08 dmunicio

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

github-actions[bot] avatar Sep 03 '25 00:09 github-actions[bot]

Not stale.

MScottBlake avatar Sep 03 '25 00:09 MScottBlake

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

github-actions[bot] avatar Oct 05 '25 00:10 github-actions[bot]

Not stale.

MScottBlake avatar Oct 05 '25 00:10 MScottBlake

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

github-actions[bot] avatar Nov 05 '25 00:11 github-actions[bot]

#notstale

n8felton avatar Nov 05 '25 00:11 n8felton