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

Commit 5c14008

Browse files
authored
feat: use bentoml config to disable prometheus (#44)
* test lambda locally * use bentoml_config file * change entrypoint string * add docs for dev * update bentoml api_server config
1 parent 6018f61 commit 5c14008

File tree

7 files changed

+73
-19
lines changed

7 files changed

+73
-19
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
AWS Lambda supports deployment via containers of upto 10gb and that is what we
2+
are using for deploying bentos into lambda. The docs can be accessed
3+
[here](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html).
4+
5+
In order for AWS to run the container in lambda they require the following
6+
- Defining a handler: this is the python code that will handle the `event` and
7+
`context` dict that is passed when ever the lambda function is invoked. Our
8+
handler is defined in ./app.py and it uses
9+
[Mangum](https://github.com/jordaneremieff/mangum) to handle the incoming
10+
requrest.
11+
- setup aws-lambda-ric: this is the runtime-interface-client that AWS has
12+
developed to interface with the Runtime API
13+
(https://github.com/aws/aws-lambda-python-runtime-interface-client). The
14+
./entry_script.sh handles invoking and loading the handler with the RIC.
15+
16+
## Terraform setup
17+
18+
AWS lambda is setup with an HTTP API infront to act as public endpoint for the
19+
lambda function. Any request that comes is passed onto the lambda function. The
20+
request is handled by mangum which transforms it into a ASGI request that can be
21+
processed by bentoml's ASGI app.
22+
23+
All the logs are in cloudwatch and IAM policies for every infra is created by
24+
the terraform template.
25+
26+
## Dev setup
27+
28+
Right now running it locally is not possible. There is a lambda Runtime Emulator
29+
that loads any handler and emulates the AWS interface but it is still work in
30+
progress.

bentoctl_lambda/aws_lambda/app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import os
22

33
from bentoml import load
4-
from bentoml._internal.configuration.containers import DeploymentContainer
54
from mangum import Mangum
65

76
API_GATEWAY_STAGE = os.environ.get("API_GATEWAY_STAGE", None)
87
print("Loading from dir...")
98
bento_service = load("./")
109
print("bento service", bento_service)
1110

12-
# Disable /metrics endpoint since promethues is not configured for use
13-
# in lambda
14-
DeploymentContainer.api_server_config.metrics.enabled.set(False)
1511
mangum_app = Mangum(bento_service.asgi_app, api_gateway_base_path=API_GATEWAY_STAGE)
5.3 MB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
api_server:
2+
metrics:
3+
enabled: False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
3+
exec /usr/local/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric $@
4+
else
5+
exec /usr/local/bin/python -m awslambdaric $@
6+
fi

bentoctl_lambda/aws_lambda/template.j2

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
{% set bento__home = "/tmp" %}
33
{% block SETUP_BENTO_ENTRYPOINT %}
44
EXPOSE 3000
5+
ENV BENTOML_CONFIG={{bento__path}}/bentoml_config.yaml
56

67
RUN --mount=type=cache,from=cached,mode=0777,target=/root/.cache/pip \
78
pip install awslambdaric==2.0.0 mangum==0.12.3
89

910
USER root
10-
11-
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
11+
ADD ./aws-lambda-rie /usr/local/bin/aws-lambda-rie
12+
RUN chmod +x /usr/local/bin/aws-lambda-rie
13+
RUN chmod +x {{bento__path}}/env/docker/entry_script.sh
14+
ENTRYPOINT ["{{bento__path}}/env/docker/entry_script.sh"]
1215
{% endblock %}

bentoctl_lambda/create_deployable.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import os
44
import shutil
5-
from sys import version_info
65
from pathlib import Path
7-
from attr import asdict
6+
from sys import version_info
87
from typing import Any
98

9+
from attr import asdict
10+
1011
if version_info >= (3, 8):
1112
from shutil import copytree
1213
else:
@@ -19,6 +20,9 @@
1920
LAMBDA_DIR = Path(os.path.dirname(__file__), "aws_lambda")
2021
TEMPLATE_PATH = LAMBDA_DIR.joinpath("template.j2")
2122
APP_PATH = LAMBDA_DIR.joinpath("app.py")
23+
ENTRY_SCRIPT = LAMBDA_DIR.joinpath("entry_script.sh")
24+
AWS_LAMBDA_RIE = LAMBDA_DIR.joinpath("aws-lambda-rie-x86")
25+
BENTOML_CONFIG_FILE = LAMBDA_DIR.joinpath("bentoml_server_config.yaml")
2226

2327

2428
def create_deployable(
@@ -49,11 +53,6 @@ def create_deployable(
4953
deployable_path = Path(destination_dir)
5054
copytree(bento_path, deployable_path, dirs_exist_ok=True)
5155

52-
if deployable_path.exists():
53-
if not overwrite_deployable:
54-
print("Using existing deployable")
55-
return str(deployable_path)
56-
5756
bento_metafile = Path(bento_path, "bento.yaml")
5857
with bento_metafile.open("r", encoding="utf-8") as metafile:
5958
info = BentoInfo.from_yaml_file(metafile)
@@ -63,15 +62,32 @@ def create_deployable(
6362

6463
dockerfile_path = deployable_path.joinpath("env", "docker", "Dockerfile")
6564
with dockerfile_path.open("w", encoding="utf-8") as dockerfile:
66-
dockerfile.write(
67-
generate_dockerfile(
68-
DockerOptions(**options).with_defaults(),
69-
str(deployable_path),
70-
use_conda=not info.conda.is_empty(),
71-
)
65+
dockerfile_generated = generate_dockerfile(
66+
DockerOptions(**options).with_defaults(),
67+
str(deployable_path),
68+
use_conda=not info.conda.is_empty(),
7269
)
70+
dockerfile.write(dockerfile_generated)
7371

7472
# copy over app.py file
7573
shutil.copy(str(APP_PATH), os.path.join(deployable_path, "app.py"))
7674

75+
# the entry_script.sh file that will be the entrypoint
76+
shutil.copy(
77+
str(ENTRY_SCRIPT),
78+
os.path.join(deployable_path, "env", "docker", "entry_script.sh"),
79+
)
80+
81+
# aws-lambda runtime-interface-emulator - check docs for more info
82+
shutil.copy(
83+
str(AWS_LAMBDA_RIE),
84+
os.path.join(deployable_path, "aws-lambda-rie"),
85+
)
86+
87+
# bentoml_config_file to dissable /metrics
88+
shutil.copy(
89+
str(BENTOML_CONFIG_FILE),
90+
os.path.join(deployable_path, "bentoml_config.yaml"),
91+
)
92+
7793
return str(deployable_path)

0 commit comments

Comments
 (0)