diff --git a/bentoctl_lambda/aws_lambda/Dockerfile b/bentoctl_lambda/aws_lambda/Dockerfile.template similarity index 87% rename from bentoctl_lambda/aws_lambda/Dockerfile rename to bentoctl_lambda/aws_lambda/Dockerfile.template index f60be79..21ffae5 100644 --- a/bentoctl_lambda/aws_lambda/Dockerfile +++ b/bentoctl_lambda/aws_lambda/Dockerfile.template @@ -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 diff --git a/bentoctl_lambda/aws_lambda/__init__.py b/bentoctl_lambda/aws_lambda/__init__.py index cc1159b..ae21be0 100644 --- a/bentoctl_lambda/aws_lambda/__init__.py +++ b/bentoctl_lambda/aws_lambda/__init__.py @@ -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"), @@ -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, diff --git a/bentoctl_lambda/aws_lambda/app.py b/bentoctl_lambda/aws_lambda/app.py index 8e26d69..207307a 100644 --- a/bentoctl_lambda/aws_lambda/app.py +++ b/bentoctl_lambda/aws_lambda/app.py @@ -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__] diff --git a/bentoctl_lambda/deploy.py b/bentoctl_lambda/deploy.py index 7348edf..b0c70aa 100644 --- a/bentoctl_lambda/deploy.py +++ b/bentoctl_lambda/deploy.py @@ -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", diff --git a/bentoctl_lambda/utils/__init__.py b/bentoctl_lambda/utils/__init__.py index 34738fb..29a70e2 100644 --- a/bentoctl_lambda/utils/__init__.py +++ b/bentoctl_lambda/utils/__init__.py @@ -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