I'm using this Dockerfile
to build an image for AWS Lambda having librosa
and ffmpeg
with python3.7
:
ARG FUNCTION_DIR="/function"
FROM python:3.7.4-slim-buster as build-image
# librosa and ffmpeg dependencies
RUN apt-get update && apt-get install -y \
software-properties-common \
libsndfile1-dev \
ffmpeg
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Copy function code
COPY app.py ${FUNCTION_DIR}
# Install the runtime interface client
RUN pip install \
--target ${FUNCTION_DIR} \
awslambdaric
# Multi-stage build: grab a fresh copy of the base image
FROM python:3.7.4-slim-buster
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the build image dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
EXPOSE 8080
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]
The build works ok, but I cannot run the image:
docker run -e AWS_LAMBDA_RUNTIME_API=localhost:9000 --rm -it -p 9000:8080 lambda:latest
Executing 'app.handler' in function directory '/function'
[ERROR] [1628271126733] LAMBDA_RUNTIME Failed to get next invocation. No Response from endpoint
Traceback (most recent call last):
File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/function/awslambdaric/__main__.py", line 21, in <module>
main(sys.argv)
File "/function/awslambdaric/__main__.py", line 17, in main
bootstrap.run(app_root, handler, lambda_runtime_api_addr)
File "/function/awslambdaric/bootstrap.py", line 399, in run
event_request = lambda_runtime_client.wait_next_invocation()
File "/function/awslambdaric/lambda_runtime_client.py", line 76, in wait_next_invocation
response_body, headers = runtime_client.next()
RuntimeError: Failed to get next
I therefore tried this simplified Dockerfile
that is based on the AWS EC2 CentOS with package manager yum
:
FROM public.ecr.aws/lambda/python:3.8
# libsndfile
RUN curl -o libsndfile-1.0.28-alt1.x86_64.rpm http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS.classic//libsndfile-1.0.28-alt1.x86_64.rpm
RUN curl -o libsndfile-devel-1.0.28-alt1.x86_64.rpm http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS.classic//libsndfile-devel-1.0.28-alt1.x86_64.rpm
#ffmpeg
RUN curl -o ffmpeg-4.4-alt5.x86_64.rpm http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS.classic//ffmpeg-4.4-alt5.x86_64.rpm
RUN yum localinstall \
libsndfile-devel-1.0.28-alt1.x86_64.rpm \
fmpeg-4.4-alt5.x86_64.rpm
# Create function directory
WORKDIR /app
# Copy function code
COPY app.py .
COPY requirements.txt .
RUN pip install -r requirements.txt
EXPOSE 8080
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD ["/app/app.handler"]
In this case, the image builds and run and I can invoke the handler:
docker run --rm -it -p 9000:8080 lambda:latest
INFO[0000] exec '/var/runtime/bootstrap' (cwd=/app, handler=)
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
but I librosa
complains about the libsndfile1
installation:
Traceback (most recent call last):
File "/var/runtime/bootstrap.py", line 452, in main
request_handler = _get_handler(handler)
File "/var/runtime/bootstrap.py", line 46, in _get_handler
m = imp.load_module(modname, file_handle, pathname, desc)
File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/app.py", line 7, in <module>
import librosa
File "/var/lang/lib/python3.8/site-packages/librosa/__init__.py", line 211, in <module>
from . import core
File "/var/lang/lib/python3.8/site-packages/librosa/core/__init__.py", line 6, in <module>
from .audio import * # pylint: disable=wildcard-import
File "/var/lang/lib/python3.8/site-packages/librosa/core/audio.py", line 8, in <module>
import soundfile as sf
File "/var/lang/lib/python3.8/site-packages/soundfile.py", line 142, in <module>
raise OSError('sndfile library not found')
OSError: sndfile library not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/runtime/bootstrap.py", line 480, in <module>
main()
File "/var/runtime/bootstrap.py", line 457, in main
lambda_runtime_client.post_init_error(to_json(error_result))
File "/var/runtime/lambda_runtime_client.py", line 42, in post_init_error
response = runtime_connection.getresponse()
File "/var/lang/lib/python3.8/http/client.py", line 1344, in getresponse
response.begin()
File "/var/lang/lib/python3.8/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/var/lang/lib/python3.8/http/client.py", line 276, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
WARN[0023] First fatal error stored in appctx: Runtime.ExitError
WARN[0023] Process 16(bootstrap) exited: Runtime exited with error: exit status 1
ERRO[0023] Init failed InvokeID= error="Runtime exited with error: exit status 1"
WARN[0023] Failed to send default error response: ErrInvalidInvokeID
ERRO[0023] INIT DONE failed: Runtime.ExitError
WARN[0023] Reset initiated: ReserveFail
The strange thing in this case (yum
and rmp
) is that the installation seems to work fine:
=> CACHED [2/9] RUN curl -o libsndfile-1.0.28-alt1.x86_64.rpm http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS.classic//libsndfile-1.0.2 0.0s
=> CACHED [3/9] RUN curl -o libsndfile-devel-1.0.28-alt1.x86_64.rpm http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS.classic//libsndfile 0.0s
=> CACHED [4/9] RUN curl -o ffmpeg-4.4-alt5.x86_64.rpm http://ftp.altlinux.org/pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS.classic//ffmpeg-4.4-alt5.x86_64 0.0s
=> CACHED [5/9] RUN yum localinstall libsndfile-devel-1.0.28-alt1.x86_64.rpm fmpeg-4.4-alt5.x86_64.rpm