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

refactor lambda #21

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ARG BENTOML_VERSION=1.0.0a1
ARG PYTHON_VERSION=3.8
FROM bentoml/bento-server:$BENTOML_VERSION-python$PYTHON_VERSION-debian-runtime
FROM bentoml/bento-server:{bentoml_version}-python{python_version}-debian-runtime

ARG UID=1034
ARG GID=1034
Expand Down
35 changes: 25 additions & 10 deletions bentoctl_lambda/aws_lambda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,36 @@

import yaml

from bentoctl_lambda.utils import get_metadata

logger = logging.getLogger(__name__)


def generate_lambda_deployable(bento_bundle_path, project_path, lambda_config):
def generate_lambda_deployable(bento_path, project_path, lambda_config):
bento_metadata = get_metadata(bento_path)
current_dir_path = os.path.dirname(__file__)

# copy bento_bundle to project_path
shutil.copytree(bento_bundle_path, project_path)

# Copy Dockerfile with added support for Lambda
shutil.copy(
os.path.join(current_dir_path, "Dockerfile"),
os.path.join(project_path, "Dockerfile-lambda"),
)

shutil.copytree(bento_path, project_path)

# Make docker file with dockerfile template
template_file = os.path.join(current_dir_path, "Dockerfile.template")
dockerfile = os.path.join(project_path, "Dockerfile-lambda")
with open(template_file, "r", encoding="utf-8") as f:
dockerfile_template = f.read()

with open(dockerfile, "w") as dockerfile:
dockerfile.write(
dockerfile_template.format(
bentoml_version=bento_metadata["bentoml_version"],
python_version=bento_metadata["python_version"],
)
)
# shutil.copy(
# os.path.join(current_dir_path, "Dockerfile"),
# os.path.join(project_path, "Dockerfile-lambda"),
# )
#
# copy the entrypoint
shutil.copy(
os.path.join(current_dir_path, "entry.sh"),
Expand Down Expand Up @@ -108,7 +123,7 @@ def generate_aws_lambda_cloudformation_template_file(
"Globals": {
"Function": {"Timeout": timeout, "MemorySize": memory_size},
"Api": {
"BinaryMediaTypes": ["image~1*"],
"BinaryMediaTypes": ["*~1*"],
"Cors": "'*'",
"Auth": {
"ApiKeyRequired": False,
Expand Down
8 changes: 4 additions & 4 deletions bentoctl_lambda/aws_lambda/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from mangum import Mangum

api_name = os.environ["BENTOML_API_NAME"]
print('loading app: ', api_name)
print('Loading from dir...')
bento_service = load('./')
print('bento service', bento_service)
print("loading app: ", api_name)
print("Loading from dir...")
bento_service = load("./")
print("bento service", bento_service)
# bento_service_api = bento_service.get_inference_api(api_name)

this_module = sys.modules[__name__]
Expand Down
2 changes: 1 addition & 1 deletion bentoctl_lambda/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def deploy(bento_path, deployment_name, deployment_spec):
template_file_path = generate_aws_lambda_cloudformation_template_file(
deployment_name=deployment_name,
project_dir=deployable_path,
api_names=list(bento_svc._apis),
api_names=list(bento_svc.apis),
bento_service_name=bento_svc.name,
docker_context=deployable_path,
docker_file="Dockerfile-lambda",
Expand Down
15 changes: 13 additions & 2 deletions bentoctl_lambda/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ def generate_docker_image_tag(registry_uri, bento_name, bento_version):
return f"{registry_uri}:{image_tag}"


def get_tag_from_path(path: str):
def get_metadata(path: str):
metadata = {}

bento = Bento.from_fs(fs.open_fs(path))
return bento.tag
metadata["tag"] = bento.tag
metadata["bentoml_version"] = ".".join(bento.info.bentoml_version.split(".")[:3])

python_version_txt_path = "env/python/version.txt"
python_version_txt_path = os.path.join(path, python_version_txt_path)
with open(python_version_txt_path, "r") as f:
python_version = f.read()
metadata["python_version"] = ".".join(python_version.split(".")[:2])

return metadata