Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

fix: use user-template to generate dockerfile #40

Merged
merged 10 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions bentoctl_lambda/aws_lambda/Dockerfile.template

This file was deleted.

12 changes: 12 additions & 0 deletions bentoctl_lambda/aws_lambda/bentoctl_user_template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends bento__dockerfile %}
{% set bento__home = "/tmp" %}
{% block SETUP_BENTO_ENTRYPOINT %}
EXPOSE 3000

RUN --mount=type=cache,mode=0777,target=/root/.cache/pip \
Copy link
Contributor

Choose a reason for hiding this comment

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

how would we start a bento again? since you are overwritting ENTRYPOINT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the CMD command is configured my the terraform template but I'll put it back here since that is less confusing

pip install awslambdaric==2.0.0 mangum==0.12.3

USER root

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
{% endblock %}
42 changes: 18 additions & 24 deletions bentoctl_lambda/create_deployable.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import os
import shutil

from bentoctl.docker_utils import DOCKERFILE_PATH
from bentoml._internal.bento.build_config import DockerOptions
from bentoml._internal.bento.gen import generate_dockerfile

path_to_aws_lambda_files = os.path.join(os.path.dirname(__file__), "./aws_lambda/")
DOCKERFILE_TEMPLATE = os.path.join(path_to_aws_lambda_files, "Dockerfile.template")
BENTOML_USER_TEMPLATE = os.path.join(
path_to_aws_lambda_files, "bentoctl_user_template.j2"
)
APP_FILE = os.path.join(path_to_aws_lambda_files, "app.py")


def generate_lambda_deployable(bento_path, bento_metadata, deployable_path):
def generate_lambda_deployable(bento_path, _, deployable_path):
# copy bento_bundle to project_path
shutil.copytree(bento_path, deployable_path)

# Make docker file with dockerfile template
dockerfile = os.path.join(deployable_path, "Dockerfile")
with open(DOCKERFILE_TEMPLATE, "r", encoding="utf-8") as f, open(
dockerfile, "w"
) as dockerfile:
dockerfile_template = f.read()
dockerfile.write(
dockerfile_template.format(
bentoml_version=bento_metadata["bentoml_version"],
python_version=bento_metadata["python_version"],
)
)
# Make docker file with user template
docker_options_for_lambda = DockerOptions(dockerfile_template=BENTOML_USER_TEMPLATE)
Copy link
Contributor

Choose a reason for hiding this comment

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

need to use amazonlinux here, and also set a python version for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

amazonlinux is not a requirement actually but we should pin the python version. we can use the same one from the bento

dockerfile_generated = generate_dockerfile(
docker_options_for_lambda.with_defaults(), use_conda=False
Copy link
Member

Choose a reason for hiding this comment

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

isn't this going to use the python version on the bentoctl build machine instead of the bento's python version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep that is a bug. Thanks for pointing it out!
since we have the python version in bento.yaml we will use that python version when generating the dockerfile

Copy link
Contributor

Choose a reason for hiding this comment

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

I will take over from here

)
dockerfile = os.path.join(deployable_path, DOCKERFILE_PATH)
with open(dockerfile, "w") as dockerfile:
dockerfile.write(dockerfile_generated)

# copy over app.py file
shutil.copy(
Expand All @@ -48,33 +50,25 @@ def create_deployable(

Returns
-------
dockerfile_path : str
path to the dockerfile.
docker_context_path : str
path to the docker context.
additional_build_args : dict
Any addition build arguments that need to be passed to the
docker build command
"""

deployable_path = os.path.join(destination_dir, "bentoctl_deployable")
docker_context_path = deployable_path
dockerfile_path = os.path.join(deployable_path, "Dockerfile")

if os.path.exists(deployable_path):
if overwrite_deployable:
print(f"Overwriting existing deployable [{deployable_path}]")
shutil.rmtree(deployable_path)
else:
print("Using existing deployable")
return dockerfile_path, docker_context_path, additional_build_args
return docker_context_path

generate_lambda_deployable(
bento_path=bento_path,
bento_metadata=bento_metadata,
deployable_path=deployable_path,
)

additional_build_args = None

return dockerfile_path, docker_context_path, additional_build_args
return docker_context_path