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

Commit c418776

Browse files
jjmachanyubozhaoaarnphm
authored
fix: use user-template to generate dockerfile (#40)
* usertemplate for aws-lambda * generate docker template * update return statements * remove dockerfile template * Update bentoctl_lambda/aws_lambda/bentoctl_user_template.j2 Co-authored-by: Aaron Pham <[email protected]> * fix: lambda fits with bentoml rc1 Signed-off-by: Aaron Pham <[email protected]> * chore: fixes Signed-off-by: Aaron Pham <[email protected]> * chore: cleanup Signed-off-by: Aaron Pham <[email protected]> * chore: types Signed-off-by: Aaron Pham <[email protected]> * chore: update types Signed-off-by: Aaron Pham <[email protected]> Co-authored-by: Bozhao <[email protected]> Co-authored-by: Aaron Pham <[email protected]>
1 parent a078af0 commit c418776

File tree

10 files changed

+85
-128
lines changed

10 files changed

+85
-128
lines changed

bentoctl_lambda/aws_lambda/Dockerfile.template

Lines changed: 0 additions & 33 deletions
This file was deleted.

bentoctl_lambda/aws_lambda/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from bentoml._internal.configuration.containers import DeploymentContainer
55
from mangum import Mangum
66

7-
API_GATEWAY_STAGE = os.environ.get('API_GATEWAY_STAGE', None)
7+
API_GATEWAY_STAGE = os.environ.get("API_GATEWAY_STAGE", None)
88
print("Loading from dir...")
99
bento_service = load("./")
1010
print("bento service", bento_service)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends bento_base_template %}
2+
{% set bento__home = "/tmp" %}
3+
{% block SETUP_BENTO_ENTRYPOINT %}
4+
EXPOSE 3000
5+
6+
RUN --mount=type=cache,from=cached,mode=0777,target=/root/.cache/pip \
7+
pip install awslambdaric==2.0.0 mangum==0.12.3
8+
9+
USER root
10+
11+
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
12+
{% endblock %}

bentoctl_lambda/create_deployable.py

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
1+
from __future__ import annotations
2+
13
import os
24
import shutil
5+
from pathlib import Path
6+
from typing import Any
37

4-
path_to_aws_lambda_files = os.path.join(os.path.dirname(__file__), "./aws_lambda/")
5-
DOCKERFILE_TEMPLATE = os.path.join(path_to_aws_lambda_files, "Dockerfile.template")
6-
APP_FILE = os.path.join(path_to_aws_lambda_files, "app.py")
7-
8-
9-
def generate_lambda_deployable(bento_path, bento_metadata, deployable_path):
10-
# copy bento_bundle to project_path
11-
shutil.copytree(bento_path, deployable_path)
12-
13-
# Make docker file with dockerfile template
14-
dockerfile = os.path.join(deployable_path, "Dockerfile")
15-
with open(DOCKERFILE_TEMPLATE, "r", encoding="utf-8") as f, open(
16-
dockerfile, "w"
17-
) as dockerfile:
18-
dockerfile_template = f.read()
19-
dockerfile.write(
20-
dockerfile_template.format(
21-
bentoml_version=bento_metadata["bentoml_version"],
22-
python_version=bento_metadata["python_version"],
23-
)
24-
)
8+
from bentoml._internal.bento.bento import BentoInfo
9+
from bentoml._internal.bento.build_config import DockerOptions
10+
from bentoml._internal.bento.gen import generate_dockerfile
11+
from bentoml._internal.utils import bentoml_cattr
2512

26-
# copy over app.py file
27-
shutil.copy(
28-
APP_FILE,
29-
os.path.join(deployable_path, "app.py"),
30-
)
13+
LAMBDA_DIR = Path(os.path.dirname(__file__), "aws_lambda")
14+
TEMPLATE_PATH = LAMBDA_DIR.joinpath("template.j2")
15+
APP_PATH = LAMBDA_DIR.joinpath("app.py")
3116

3217

3318
def create_deployable(
34-
bento_path, destination_dir, bento_metadata, overwrite_deployable
35-
):
19+
bento_path: str,
20+
destination_dir: str,
21+
bento_metadata: dict[str, Any],
22+
overwrite_deployable: bool,
23+
) -> str:
3624
"""
3725
The deployable is the bento along with all the modifications (if any)
3826
requried to deploy to the cloud service.
@@ -48,33 +36,39 @@ def create_deployable(
4836
4937
Returns
5038
-------
51-
dockerfile_path : str
52-
path to the dockerfile.
5339
docker_context_path : str
5440
path to the docker context.
55-
additional_build_args : dict
56-
Any addition build arguments that need to be passed to the
57-
docker build command
5841
"""
5942

60-
deployable_path = os.path.join(destination_dir, "bentoctl_deployable")
61-
docker_context_path = deployable_path
62-
dockerfile_path = os.path.join(deployable_path, "Dockerfile")
43+
deployable_path = Path(destination_dir)
44+
shutil.copytree(bento_path, deployable_path, dirs_exist_ok=True)
6345

64-
if os.path.exists(deployable_path):
65-
if overwrite_deployable:
66-
print(f"Overwriting existing deployable [{deployable_path}]")
67-
shutil.rmtree(deployable_path)
68-
else:
46+
if deployable_path.exists():
47+
if not overwrite_deployable:
6948
print("Using existing deployable")
70-
return dockerfile_path, docker_context_path, additional_build_args
49+
return str(deployable_path)
50+
51+
bento_metafile = Path(bento_path, "bento.yaml")
52+
with bento_metafile.open("r", encoding="utf-8") as metafile:
53+
info = BentoInfo.from_yaml_file(metafile)
7154

72-
generate_lambda_deployable(
73-
bento_path=bento_path,
74-
bento_metadata=bento_metadata,
75-
deployable_path=deployable_path,
76-
)
55+
options = bentoml_cattr.unstructure(info.docker)
56+
options["dockerfile_template"] = TEMPLATE_PATH
7757

78-
additional_build_args = None
58+
dockerfile_path = deployable_path.joinpath("env", "docker", "Dockerfile")
59+
with dockerfile_path.open("w", encoding="utf-8") as dockerfile:
60+
dockerfile.write(
61+
generate_dockerfile(
62+
DockerOptions(**options).with_defaults(),
63+
str(deployable_path),
64+
use_conda=any(
65+
i is not None
66+
for i in bentoml_cattr.unstructure(info.conda).values()
67+
),
68+
)
69+
)
70+
71+
# copy over app.py file
72+
shutil.copy(str(APP_PATH), os.path.join(deployable_path, "app.py"))
7973

80-
return dockerfile_path, docker_context_path, additional_build_args
74+
return str(deployable_path)

bentoctl_lambda/parameters.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from collections import UserDict
22

3-
from bentoctl_lambda.schemas import OPERATOR_SCHEMA
4-
53
DEPLOYMENT_PARAMS_WARNING = """# This file is maintained automatically by
64
# "bentoctl generate" and "bentoctl build" commands.
75
# Manual edits may be lost the next time these commands are run.

bentoctl_lambda/schemas.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

bentoctl_lambda/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22

3-
import fs
43
import boto3
4+
import fs
55
from bentoml.bentos import Bento
66

77

operator_config.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
from bentoctl_lambda.schemas import OPERATOR_SCHEMA
1+
OPERATOR_SCHEMA = {
2+
"region": {
3+
"required": True,
4+
"type": "string",
5+
"default": "us-west-1",
6+
"help_message": "AWS region for Lambda deployment",
7+
},
8+
"timeout": {
9+
"required": False,
10+
"type": "integer",
11+
"coerce": int,
12+
"default": 10,
13+
"help_message": "Timeout per request",
14+
},
15+
"memory_size": {
16+
"required": False,
17+
"type": "integer",
18+
"coerce": int,
19+
"default": 512,
20+
"help_message": "The memory for your function, set a value between 128 MB and 10,240 MB in 1-MB increments",
21+
},
22+
}
223

324
OPERATOR_NAME = "aws-lambda"
425

tests/classifier.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# iris_classifier.py
2-
from bentoml import env, api, BentoService
3-
from bentoml.adapters import DataframeInput, JsonInput, FileInput
4-
from bentoml.types import JsonSerializable, FileLike
2+
from bentoml import BentoService, api, env
3+
from bentoml.adapters import DataframeInput, FileInput, JsonInput
4+
from bentoml.types import FileLike, JsonSerializable
55

66

77
@env(infer_pip_packages=True)
88
class TestService(BentoService):
9-
109
@api(input=DataframeInput(), batch=True)
1110
def dfapi(self, df):
1211
print(df)

tests/test_api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import os
2-
import sys
32
import shutil
3+
import sys
44
import tempfile
55
import time
66

77
import requests
8-
from pandas import DataFrame
9-
108
from classifier import TestService
9+
from pandas import DataFrame
1110

1211
sys.path.append("./")
12+
from delete import delete
1313
from deploy import deploy
1414
from describe import describe
15-
from delete import delete
1615

1716

1817
class Setup:

0 commit comments

Comments
 (0)