OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

AWS Lambda with container: response time is very slow

  • Thread starter Thread starter Alexey Ignatyev
  • Start date Start date
A

Alexey Ignatyev

Guest
I am trying to make container with docker which can run with AWS Lambda Service.

This is some information:

  1. Container was build with this guide: https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
  2. when I run built container on my local machine: response time is 3 seconds
  3. when I execute lambda function in AWS Lambda: response time is 20-25 seconds

Here is a Dockerfile:

Code:
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:

Code:
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.
<p>I am trying to make container with docker which can run with AWS Lambda Service.</p>
<p>This is some information:</p>
<ol>
<li>Container was build with this guide: <a href="https://docs.aws.amazon.com/lambda/latest/dg/python-image.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/lambda/latest/dg/python-image.html</a></li>
<li>when I run built container on my local machine: response time is 3 seconds</li>
<li>when I execute lambda function in AWS Lambda: response time is 20-25 seconds</li>
</ol>
<p>Here is a Dockerfile:</p>
<pre><code>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" ]
</code></pre>
<p>Here is lambda function:</p>
<pre><code>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}
</code></pre>
<p>Reason why I can't use Lambda layers is that size of Python layers necesary to run this function is more than 256 MB.</p>
<p>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.</p>
<p>How to improve response time? Am I doing something wrong?</p>
<p>Thanks.</p>
 
Top