terraform-aws-modules/terraform-aws-lambda

Poetry build does not use poetry virtual environment python version

mleziva opened this issue · 7 comments

Description

When building and packaging a python lambda function, the python version of the shell must match the lambda functions configured runtime or the package will fail.

  • ✋ I have searched the open/closed issues and my issue is not listed.

Versions

  • Module version [Required]: 5.2.0

Reproduction Code [Required]

Steps to reproduce the behavior:

install pyenv
install poetry
create a new poetry project with python version 3.9
switch shell python version to 3.11
run terraform apply lambda function to package and deploy the new poetry python application

Expected behavior

Application should package and deploy using the poetry virtual environment

Actual behavior

Package fails because package.py tries to use pip with the currently set python version instead of the version configured for the poetry virtual environment

Terminal Output Screenshot(s)

│ Error: local-exec provisioner error
│ 
│   with module.lambda_function.null_resource.archive[0],
│   on .terraform/modules/lambda_function/package.tf line 67, in resource "null_resource" "archive":
│   67:   provisioner "local-exec" {
│ 
│ Error running command '.builds/dc9e2656f15915b3d81d870403a9cc6d652b1afb389b981cc95a08129d483bd6.plan.json': exit status 1. Output: zip: creating '.builds/dc9e2656f15915b3d81d870403a9cc6d652b1afb389b981cc95a08129d483bd6.zip' archive
│ Installing python dependencies with poetry & pip: ../poc-01/poetry.lock
│ > mktemp -d terraform-aws-lambda-XXXXXXXX # /var/folders/wr/dtf1pfl55p7cdcw2fy_cncxh0000gr/T/terraform-aws-lambda-5ufgf1jr
│ Using poetry lock file: ../poc-01/poetry.lock
│ > cd /var/folders/wr/dtf1pfl55p7cdcw2fy_cncxh0000gr/T/terraform-aws-lambda-5ufgf1jr
│ > [['poetry', 'config', '--no-interaction', 'virtualenvs.create', 'true'], ['poetry', 'config', '--no-interaction', 'virtualenvs.in-project', 'true'], ['poetry', 'export', '--format', 'requirements.txt', '--output', 'requirements.txt', '--with-credentials'],
│ ['python3.9', '-m', 'pip', 'install', '--no-compile', '--no-deps', '--prefix=', '--target=.', '--requirement=requirements.txt']]
│ pyenv: python3.9: command not found

Additional context

Looking at package.py, I can see that it uses pip to install so it can install to a specific directory

  python_exec,
                    "-m",
                    "pip",
                    "install",
                    "--no-compile",
                    "--no-deps",
                    "--prefix=",
                    "--target=.",
                    "--requirement=requirements.txt",

However, the python_exec variable is set based on the function runtime without regards to the poetry virtual environment that is configured. So it is possible that the poetry virtual environment may be configured to use python3.9 and the app will build and run successfully using poetry, but when the user executes terraform to deploy it, it could fail because the users current python version does not match the poetry version.

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

This issue was automatically closed because of stale in 10 days

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.