Skip to content

Commit da26dc2

Browse files
committed
feat: support installing dependencies with poetry
1 parent 185858d commit da26dc2

File tree

11 files changed

+336
-26
lines changed

11 files changed

+336
-26
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,10 @@ Few notes:
412412
!abc/def/hgk/.* # Filter out again in abc/def/hgk sub folder
413413
```
414414

415-
- `commands` - List of commands to run. If specified, this argument overrides `pip_requirements`.
415+
- `commands` - List of commands to run. If specified, this argument overrides `pip_requirements` and `poetry_install`.
416416
- `:zip [source] [destination]` is a special command which creates content of current working directory (first argument) and places it inside of path (second argument).
417417
- `pip_requirements` - Controls whether to execute `pip install`. Set to `false` to disable this feature, `true` to run `pip install` with `requirements.txt` found in `path`. Or set to another filename which you want to use instead.
418+
- `poetry_install` - Controls whether to execute `poetry install`. Set to `false` to disable this feature, `true` to run `poetry install` with existing `pyproject.toml` and optionally `poetry.lock` found in `path`.
418419
- `prefix_in_zip` - If specified, will be used as a prefix inside zip-archive. By default, everything installs into the root of zip-archive.
419420

420421
### Building in Docker

examples/build-package/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,25 @@ Note that this example may create resources which cost money. Run `terraform des
2727

2828
| Name | Version |
2929
|------|---------|
30-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2 |
30+
| <a name="provider_random"></a> [random](#provider\_random) | 3.1.0 |
3131

3232
## Modules
3333

3434
| Name | Source | Version |
3535
|------|--------|---------|
36-
| <a name="module_lambda_function_from_package"></a> [lambda\_function\_from\_package](#module\_lambda\_function\_from\_package) | ../../ | |
37-
| <a name="module_lambda_layer"></a> [lambda\_layer](#module\_lambda\_layer) | ../../ | |
38-
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | |
39-
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | |
40-
| <a name="module_package_file"></a> [package\_file](#module\_package\_file) | ../../ | |
41-
| <a name="module_package_file_with_pip_requirements"></a> [package\_file\_with\_pip\_requirements](#module\_package\_file\_with\_pip\_requirements) | ../../ | |
42-
| <a name="module_package_with_commands_and_patterns"></a> [package\_with\_commands\_and\_patterns](#module\_package\_with\_commands\_and\_patterns) | ../../ | |
43-
| <a name="module_package_with_docker"></a> [package\_with\_docker](#module\_package\_with\_docker) | ../../ | |
44-
| <a name="module_package_with_patterns"></a> [package\_with\_patterns](#module\_package\_with\_patterns) | ../../ | |
45-
| <a name="module_package_with_pip_requirements_in_docker"></a> [package\_with\_pip\_requirements\_in\_docker](#module\_package\_with\_pip\_requirements\_in\_docker) | ../../ | |
36+
| <a name="module_lambda_function_from_package"></a> [lambda\_function\_from\_package](#module\_lambda\_function\_from\_package) | ../../ | n/a |
37+
| <a name="module_lambda_layer"></a> [lambda\_layer](#module\_lambda\_layer) | ../../ | n/a |
38+
| <a name="module_lambda_layer_poetry"></a> [lambda\_layer\_poetry](#module\_lambda\_layer\_poetry) | ../../ | n/a |
39+
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
40+
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |
41+
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a |
42+
| <a name="module_package_dir_without_poetry_install"></a> [package\_dir\_without\_poetry\_install](#module\_package\_dir\_without\_poetry\_install) | ../../ | n/a |
43+
| <a name="module_package_file"></a> [package\_file](#module\_package\_file) | ../../ | n/a |
44+
| <a name="module_package_file_with_pip_requirements"></a> [package\_file\_with\_pip\_requirements](#module\_package\_file\_with\_pip\_requirements) | ../../ | n/a |
45+
| <a name="module_package_with_commands_and_patterns"></a> [package\_with\_commands\_and\_patterns](#module\_package\_with\_commands\_and\_patterns) | ../../ | n/a |
46+
| <a name="module_package_with_docker"></a> [package\_with\_docker](#module\_package\_with\_docker) | ../../ | n/a |
47+
| <a name="module_package_with_patterns"></a> [package\_with\_patterns](#module\_package\_with\_patterns) | ../../ | n/a |
48+
| <a name="module_package_with_pip_requirements_in_docker"></a> [package\_with\_pip\_requirements\_in\_docker](#module\_package\_with\_pip\_requirements\_in\_docker) | ../../ | n/a |
4649

4750
## Resources
4851

examples/build-package/main.tf

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,50 @@ resource "random_pet" "this" {
1717
# Build packages
1818
#################
1919

20-
# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime)
20+
# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime with requirements.txt present)
2121
module "package_dir" {
2222
source = "../../"
2323

2424
create_function = false
2525

26-
runtime = "python3.8"
27-
source_path = "${path.module}/../fixtures/python3.8-app1"
26+
build_in_docker = true
27+
runtime = "python3.8"
28+
source_path = "${path.module}/../fixtures/python3.8-app1"
29+
artifacts_dir = "${path.root}/builds/package_dir/"
30+
}
31+
32+
# Create zip-archive of a single directory where "poetry install" will also be executed
33+
module "package_dir_poetry" {
34+
source = "../../"
35+
36+
create_function = false
37+
38+
build_in_docker = true
39+
runtime = "python3.8"
40+
source_path = [
41+
{
42+
path = "${path.module}/../fixtures/python3.8-app-poetry"
43+
patterns = [
44+
"!(.*/)*__pycache__/.*",
45+
"!_distutils_hack/.*",
46+
"!.pytest_cache/.*",
47+
"!.venv/.*",
48+
"!_virtualenv.pth",
49+
"!_virtualenv.py",
50+
"!distutils-precedence.pth",
51+
"!easy_install.py",
52+
"!(pip|pip-.*)(\\.virtualenv|/.*)",
53+
"!(pkg_resources|pkg_resources-.*)(\\.virtualenv|/.*)",
54+
"!(setuptools|setuptools-.*)(\\.virtualenv|/.*)",
55+
"!(wheel|wheel-.*)(\\.virtualenv|/.*)",
56+
]
57+
poetry_install = true
58+
}
59+
]
60+
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
2861
}
2962

30-
# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
63+
# Create zip-archive of a single directory without running "pip install" (default for python runtime with requirements.txt present)
3164
module "package_dir_without_pip_install" {
3265
source = "../../"
3366

@@ -227,6 +260,44 @@ module "lambda_layer" {
227260
build_in_docker = true
228261
runtime = "python3.8"
229262
docker_file = "${path.module}/../fixtures/python3.8-app1/docker/Dockerfile"
263+
docker_image = "lambci/lambda:build-python3.8"
264+
artifacts_dir = "${path.root}/builds/lambda_layer/"
265+
}
266+
267+
module "lambda_layer_poetry" {
268+
source = "../../"
269+
270+
create_layer = true
271+
layer_name = "${random_pet.this.id}-layer-poetry-dockerfile"
272+
compatible_runtimes = ["python3.8"]
273+
274+
source_path = [
275+
{
276+
path = "${path.module}/../fixtures/python3.8-app-poetry"
277+
patterns = [
278+
"!(.*/)*__pycache__/.*",
279+
"!_distutils_hack/.*",
280+
"!.pytest_cache/.*",
281+
"!.venv/.*",
282+
"!_virtualenv.pth",
283+
"!_virtualenv.py",
284+
"!distutils-precedence.pth",
285+
"!easy_install.py",
286+
"!(pip|pip-.*)(\\.virtualenv|/.*)",
287+
"!(pkg_resources|pkg_resources-.*)(\\.virtualenv|/.*)",
288+
"!(setuptools|setuptools-.*)(\\.virtualenv|/.*)",
289+
"!(wheel|wheel-.*)(\\.virtualenv|/.*)",
290+
]
291+
poetry_install = true
292+
}
293+
]
294+
hash_extra = "extra-hash-to-prevent-conflicts-with-module.package_dir"
295+
296+
build_in_docker = true
297+
runtime = "python3.8"
298+
docker_file = "${path.module}/../fixtures/python3.8-poetry/docker/Dockerfile"
299+
docker_image = "lambci/lambda:build-python3.8"
300+
artifacts_dir = "${path.root}/builds/lambda_layer_poetry/"
230301
}
231302

232303
#######################
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This file should not be included in archive.
2+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def lambda_handler(event, context):
2+
print("Hello from app1!")
3+
4+
return event

examples/fixtures/python3.8-app-poetry/poetry.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "python3.8-app-poetry"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Your Name <you@example.com>"]
6+
7+
[tool.poetry.dependencies]
8+
python = "^3.6"
9+
colorful = "^0.5.4"
10+
11+
[tool.poetry.dev-dependencies]
12+
13+
[build-system]
14+
requires = ["poetry-core>=1.0.0"]
15+
build-backend = "poetry.core.masonry.api"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build-system]
2+
build-backend = "dummy"

0 commit comments

Comments
 (0)