I am trying to make container with docker which can run with AWS Lambda Service.
This is some information:
- Container was build with this guide: https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
- when I run built container on my local machine: response time is 3 seconds
- when I execute lambda function in AWS Lambda: response time is 20-25 seconds
Here is a Dockerfile:
FROM public.ecr.aws/lambda/python:3.9
RUN yum -y install gcc gcc-c++
RUN pip install numpy==1.23.0
RUN pip install k_means_constrained
RUN pip install matplotlib
RUN pip check
COPY lambda_function.py ${LAMBDA_TASK_ROOT}
CMD [ "lambda_function.handler" ]
Here is lambda function:
from k_means_constrained import KMeansConstrained
import io
import json
import numpy as np
import matplotlib.pyplot as plt
import base64
def handler(event, context):
kmeans = KMeansConstrained(n_clusters=event['clusters'], size_min=event['cluster_min_size'], size_max=event['cluster_max_size'], random_state=0)
kmeans.fit_predict(np.array(event['coords']))
if event['plot']:
colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'white']
colors_len = len(colors)
index = 0
fig = plt.figure()
for coord in event['coords']:
plt.plot(coord[0], coord[1], '*', color=colors[kmeans.labels_[index] % colors_len])
index = index + 1
my_stringIObytes = io.BytesIO()
plt.savefig(my_stringIObytes, format='png')
plt.close()
plt.cla()
plt.clf()
my_stringIObytes.seek(0)
my_base64_pngData = base64.b64encode(my_stringIObytes.read()).decode()
else:
my_base64_pngData = '';
labels = ' '.join(str(x) for x in kmeans.labels_);
return {'labels': labels, 'png': my_base64_pngData}
Reason why I can’t use Lambda layers is that size of Python layers necesary to run this function is more than 256 MB.
I understand there is a cold start for lambda functions, but I tried to run two concurrent requests for 20 minutes – and response time isn’t improving.
How to improve response time? Am I doing something wrong?
Thanks.