I am new to lambda wherein trying to use lambda with docker image. I have the requirement to use my own docker image as base one and not the aws provided lambda images. Below is my docker file and base image is python:3.9.6. Bur i am getting below error when i try to execute lambda function.
I am referring below link to do the below implementation.AWS documentation link
# Define function directory
ARG FUNCTION_DIR="/function/"
FROM python:3.9.6 as build-image
# 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/* ${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.9.6
# 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}
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "lambda_function.lambda_handler" ]
Error
raceback (most recent call last):
File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/function/awslambdaric/__main__.py", line 20, in <module>
main(sys.argv)
File "/function/awslambdaric/__main__.py", line 14, in main
lambda_runtime_api_addr = os.environ["AWS_LAMBDA_RUNTIME_API"]
File "/usr/local/lib/python3.10/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'AWS_LAMBDA_RUNTIME_API'
Has anyone faced this issue earlier? how can we solve it?
2
Answers
If you really hunt in the AWS documentation they recommend using this as a framework for creating custom docker images: https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/
You have to install an additional file https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie and then setup a script entry.sh and it works
You are building a custom docker image if I am correct.
If you want to test locally you need to use the Runtime Interface Emulator (RIE).
I ran in a similar issue and solved it through the Testing Images AWS documentation
You need to add the RIE in your build:
And alter the entrypoint:
Beware this instance will not work in lambda but only in simulation as suggested in the documentation.
After you docker image is built, you can start it:
And then you can try sending a test request: