Skip to content

Commit

Permalink
feat: Support installing dependencies with poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat committed Jun 7, 2022
1 parent e7bfddf commit 608ffa4
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 13 deletions.
2 changes: 2 additions & 0 deletions examples/build-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ Note that this example may create resources which cost money. Run `terraform des
| <a name="module_lambda_function_from_package"></a> [lambda\_function\_from\_package](#module\_lambda\_function\_from\_package) | ../../ | n/a |
| <a name="module_lambda_layer"></a> [lambda\_layer](#module\_lambda\_layer) | ../../ | n/a |
| <a name="module_lambda_layer_pip_requirements"></a> [lambda\_layer\_pip\_requirements](#module\_lambda\_layer\_pip\_requirements) | ../.. | n/a |
| <a name="module_lambda_layer_poetry"></a> [lambda\_layer\_poetry](#module\_lambda\_layer\_poetry) | ../../ | n/a |
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |
| <a name="module_package_dir_with_npm_install"></a> [package\_dir\_with\_npm\_install](#module\_package\_dir\_with\_npm\_install) | ../../ | n/a |
| <a name="module_package_dir_without_npm_install"></a> [package\_dir\_without\_npm\_install](#module\_package\_dir\_without\_npm\_install) | ../../ | n/a |
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a |
Expand Down
59 changes: 53 additions & 6 deletions examples/build-package/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,52 @@ resource "random_pet" "this" {
# Build packages
#################

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

create_function = false

runtime = "python3.8"
source_path = "${path.module}/../fixtures/python3.8-app1"
build_in_docker = true
runtime = "python3.8"
source_path = "${path.module}/../fixtures/python3.8-app1"
artifacts_dir = "${path.root}/builds/package_dir/"
}

# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime) and set temporary directory for pip install
# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime with requirements.txt present) and set temporary directory for pip install
module "package_dir_pip_dir" {
source = "../../"

create_function = false

runtime = "python3.8"
build_in_docker = true
runtime = "python3.8"
source_path = [{
path = "${path.module}/../fixtures/python3.8-app1"
pip_tmp_dir = "${path.cwd}/../fixtures"
pip_requirements = "${path.module}/../fixtures/python3.8-app1/requirements.txt"
}]
artifacts_dir = "${path.root}/builds/package_dir_pip_dir/"
}

# Create zip-archive of a single directory where "poetry install" will also be executed
module "package_dir_poetry" {
source = "../../"

create_function = false

build_in_docker = true
runtime = "python3.8"
docker_image = "build-python3.8-poetry"
docker_file = "${path.module}/../fixtures/python3.8-app-poetry/docker/Dockerfile"

source_path = [
{
path = "${path.module}/../fixtures/python3.8-app-poetry"
poetry_install = true
}
]
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
}

# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
Expand Down Expand Up @@ -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"
docker_file = "${path.module}/../fixtures/python3.8-app1/docker/Dockerfile"
artifacts_dir = "${path.root}/builds/lambda_layer/"
}

module "lambda_layer_poetry" {
source = "../../"

create_layer = true
layer_name = "${random_pet.this.id}-layer-poetry-dockerfile"
compatible_runtimes = ["python3.8"]

source_path = [
{
path = "${path.module}/../fixtures/python3.8-app-poetry"
poetry_install = true
}
]
hash_extra = "extra-hash-to-prevent-conflicts-with-module.package_dir"

build_in_docker = true
runtime = "python3.8"
docker_image = "build-python3.8-poetry"
docker_file = "${path.module}/../fixtures/python3.8-app-poetry/docker/Dockerfile"
artifacts_dir = "${path.root}/builds/lambda_layer_poetry/"
}

#######################
Expand Down
6 changes: 6 additions & 0 deletions examples/fixtures/python3.8-app-poetry/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM public.ecr.aws/sam/build-python3.9

LABEL maintainer="Betajob AS" \
description="Patched AWS Lambda build container"

RUN pip install poetry==1.1.13
1 change: 1 addition & 0 deletions examples/fixtures/python3.8-app-poetry/ignore_please.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file should not be included in archive.
4 changes: 4 additions & 0 deletions examples/fixtures/python3.8-app-poetry/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def lambda_handler(event, context):
print("Hello from app1!")

return event
33 changes: 33 additions & 0 deletions examples/fixtures/python3.8-app-poetry/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/fixtures/python3.8-app-poetry/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "python3.8-app-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.6"
colorful = "^0.5.4"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
4 changes: 2 additions & 2 deletions examples/fixtures/python3.8-app1/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM lambci/lambda:build-python3.8 as build
FROM public.ecr.aws/sam/build-python3.8 as build

LABEL maintainer="Betajob AS" \
description="Patched AWS Lambda build container"
Expand All @@ -20,7 +20,7 @@ RUN \
&& rpmbuild -ba SPECS/automake.spec --nocheck \
&& yum install -y RPMS/noarch/*

FROM lambci/lambda:build-python3.8
FROM public.ecr.aws/sam/build-python3.8
COPY --from=build /root/rpmbuild/RPMS/noarch/*.rpm .
RUN yum install -y *.rpm \
&& rm *.rpm
2 changes: 2 additions & 0 deletions examples/fixtures/unittests/pyproject-unknown.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
build-backend = "dummy"
2 changes: 1 addition & 1 deletion examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ module "lambda_function" {
# docker_with_ssh_agent = true
# docker_file = "${path.module}/../fixtures/python3.8-app1/docker/Dockerfile"
# docker_build_root = "${path.module}/../../docker"
# docker_image = "lambci/lambda:build-python3.8"
# docker_image = "public.ecr.aws/sam/build-python3.8"
}

####
Expand Down
Loading

0 comments on commit 608ffa4

Please sign in to comment.