Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support installing poetry dependencies with pip #311

Merged
merged 8 commits into from
Oct 31, 2022

Conversation

pdecat
Copy link
Contributor

@pdecat pdecat commented May 17, 2022

Description

This PR adds support for installing poetry dependencies with pip --no-deps for projects with pyproject.toml and optionally poetry.lock files.

We use pip as as poetry does not currently provide a proper way to isolate installed dependencies from virtualenv (pip's --target option). --no-deps disables pip's dependency resolver as poetry already pinned all dependencies.

This is a continuation of #186

Motivation and Context

poetry's is more and more used and has a robust dependency resolver.

Breaking Changes

New poetry install step is not enabled by default and requires explicitly setting poetry_install = True in the source block.

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request
cd examples/build-package
terraform init
rm -rf builds/lambda_layer/ && terraform apply -target module.lambda_layer -auto-approve
rm -rf builds/lambda_layer_poetry/ && terraform apply -target module.lambda_layer_poetry -auto-approve
rm -rf builds/package_dir/ && terraform apply -target module.package_dir -auto-approve
rm -rf builds/package_dir_poetry/ && terraform apply -target module.package_dir_poetry -auto-approve
terraform destroy

@@ -278,8 +302,31 @@ module "lambda_layer" {

build_in_docker = true
runtime = "python3.8"
docker_image = "public.ecr.aws/sam/build-python3.8"
docker_image = "build-python3.8"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If public.ecr.aws/sam/build-python3.8 already exists locally, the docker_file is not used.

Comment on lines 757 to 773
pip_requirements_step(
os.path.join(path, 'requirements.txt'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pip_requirements_step() already appends the filename if path is a directory:

Suggested change
pip_requirements_step(
os.path.join(path, 'requirements.txt'))
pip_requirements_step(path)

@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Jul 18, 2022
@pdecat
Copy link
Contributor Author

pdecat commented Jul 18, 2022

Still current

@github-actions github-actions bot removed the stale label Jul 19, 2022
@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@pdecat
Copy link
Contributor Author

pdecat commented Aug 23, 2022

Still current

@1davidmichael
Copy link

I primarily use poetry with Python and appreciate this PR. I hope it is merged at some point in the future. Thanks for this work!

@antonbabenko
Copy link
Member

@pdecat Please let us know if/when this PR is ready for review and merge?

I expect that other users (including @1davidmichael @panessa @Tonkonozhenko) will be able to help with the review and testing.

@pdecat
Copy link
Contributor Author

pdecat commented Sep 23, 2022

Hi, I believe it is ready for review and testing by others.

Copy link
Contributor

@panessa panessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added support for http-basic auth for poetry. Code was not taking in consideration poetry.toml file

package.py Outdated Show resolved Hide resolved
package.py Outdated Show resolved Hide resolved
package.py Outdated Show resolved Hide resolved
package.py Outdated Show resolved Hide resolved
package.py Show resolved Hide resolved
examples/build-package/main.tf Outdated Show resolved Hide resolved
Copy link
Contributor Author

@pdecat pdecat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @panessa, thanks for the review! I've integrated your suggested changes, please take a look.

examples/build-package/main.tf Outdated Show resolved Hide resolved
@pdecat pdecat force-pushed the support_poetry branch 2 times, most recently from b7bdc36 to bdbbb0f Compare September 29, 2022 12:15
@pdecat
Copy link
Contributor Author

pdecat commented Sep 29, 2022

Hi @antonbabenko, some Github Actions jobs failed because of network errors. Mind to restart them? 🙏

@antonbabenko
Copy link
Member

@pdecat Restarted GitHub Actions, and all checks are green. I have scheduled to revive this PR on the 13th of October (after HashiConf). Hopefully, everyone else will be addressing all the comments and do a review, too.

Copy link
Member

@antonbabenko antonbabenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried to test it locally after #362 was merged, and apparently, it broke package.py you've created.

The error message I got during terraform destroy after the successful run of rm -rf builds/lambda_layer/ && terraform apply -target module.lambda_layer:

│     npm_requirements_step(
│   File ".../terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 709, in npm_requirements_step
│     raise RuntimeError(
│ RuntimeError: Nodejs interpreter version equal to
│ defined lambda runtime (nodejs14.x) should be available
│ in system PATH
│ 
│ State: exit status 1

Could you please fix that and update README.md in the root to showcase poetry support in this section and maybe also in other places where it makes sense?

@@ -0,0 +1,10 @@
from package import get_build_system_from_pyproject_toml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this file from the root to keep it clean?

Also, could you please update README with some instructions on how to run these? Or just delete it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved python unit tests to tests/ folder, add instructions to README.

And also added Github Action to run these tests against all supported python versions.

@@ -0,0 +1,2 @@
[build-system]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this file, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is needed by python unit tests. Moved it too.

examples/build-package/main.tf Show resolved Hide resolved
@pdecat pdecat force-pushed the support_poetry branch 2 times, most recently from fd8940d to 09d78ca Compare October 22, 2022 17:26
@pdecat
Copy link
Contributor Author

pdecat commented Oct 22, 2022

I just tried to test it locally after terraform-linters/tflint#362 was merged, and apparently, it broke package.py you've created.

The error message I got during terraform destroy after the successful run of rm -rf builds/lambda_layer/ && terraform apply -target module.lambda_layer:

│     npm_requirements_step(
│   File ".../terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 709, in npm_requirements_step
│     raise RuntimeError(
│ RuntimeError: Nodejs interpreter version equal to
│ defined lambda runtime (nodejs14.x) should be available
│ in system PATH
│ 
│ State: exit status 1

The same errors happen using current master branch for all examples that do not have build_in_docker = true for which the configured runtime is not available locally.

I see at least 3 solutions:

  1. install all runtimes used in the examples
  2. update all examples to only use locally available runtimes
  3. remove the runtime checks for the examples

As a work-around for now, only destroy the modules that were applied with resource targeting:

terraform destroy -target module.lambda_layer
terraform destroy -target module.lambda_layer_poetry
terraform destroy -target module.package_dir
terraform destroy -target module.package_dir_poetry

@pdecat pdecat force-pushed the support_poetry branch 3 times, most recently from 36db3c8 to 1f960f6 Compare October 24, 2022 22:20
@pdecat
Copy link
Contributor Author

pdecat commented Oct 24, 2022

The Pre-Commit / Max TF pre-commit check is failing with a weird error:

Terraform validate with tflint...........................................Failed
- hook id: terraform_tflint
- exit code: 1

Command 'tflint --init' successfully done:




TFLint in modules/deploy/:
Failed to check ruleset; Failed to check `terraform_deprecated_index` rule: main.tf:52,8-9: Invalid character; This character is not used within the language., and 57 other diagnostic(s)

See https://github.com/terraform-aws-modules/terraform-aws-lambda/actions/runs/3312065827/jobs/5468288824

And does not only affect jobs from this PR, other PRs are also affected, see https://github.com/terraform-aws-modules/terraform-aws-lambda/actions/runs/3313158688/jobs/5470763712

Edit: this seems to be caused by terraform-linters/tflint-ruleset-terraform#42

@pdecat
Copy link
Contributor Author

pdecat commented Oct 26, 2022

Pre-commit hooks should be fixed now: https://github.com/terraform-linters/tflint/releases/tag/v0.42.2

Copy link
Member

@antonbabenko antonbabenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works like a charm :) Thank you for your contribution!

@antonbabenko antonbabenko merged commit 398ae5a into terraform-aws-modules:master Oct 31, 2022
antonbabenko pushed a commit that referenced this pull request Oct 31, 2022
## [4.3.0](v4.2.2...v4.3.0) (2022-10-31)

### Features

* Support installing poetry dependencies with pip ([#311](#311)) ([398ae5a](398ae5a))
@antonbabenko
Copy link
Member

This PR is included in version 4.3.0 🎉

@pdecat pdecat deleted the support_poetry branch October 31, 2022 14:47
@Tonkonozhenko
Copy link

@pdecat @antonbabenko thank you for the work! we'll use it asap

@github-actions
Copy link

github-actions bot commented Dec 1, 2022

I'm going to lock this pull request 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 related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 1, 2022
@antonbabenko
Copy link
Member

This issue has been resolved in version 4.16.0 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants