13

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
  • Have you set it in the environment variable>AWS_LAMBDA_RUNTIME_API? Commented Feb 15, 2022 at 13:17
  • 1
    I think, we don't need to set this env variable as , lambda does it automatically. I tried to set like ENV AWS_LAMBDA_RUNTIME_API=3.9. But still didn't work Commented Feb 15, 2022 at 13:40

2 Answers 2

8

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:

RUN mkdir -p ~/.aws-lambda-rie && curl -Lo ~/.aws-lambda-rie/aws-lambda-rie \
https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie \
&& chmod +x ~/.aws-lambda-rie/aws-lambda-rie  

And alter the entrypoint:

ENTRYPOINT [ "~/.aws-lambda-rie/aws-lambda-rie","/usr/local/bin/python", "-m", "awslambdaric" ]

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:

docker run -p 9000:8080 your-image

And then you can try sending a test request:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Would you mind explaining the 2015-03-31 in the last URL? Also, I had to remove ~ from all paths to get it to work.
@RamiAlloush not sure about what follows. AWS has different API versions that are identified by their dates. You can see them if you just install an AWS SDK somewhere and check the data folder. The AWS Lambda tool must have a similar version and the endpoint expects it in the URL
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.