Search code examples
pythondockeraws-lambdaamazon-ecr

Lambda function completes run. Then runs again and crashes


I have a lambda function that calls a docker image from ECR. I ran the docker image on my local machine and it runs fine but the moment test on Lambda it runs and finishes and then runs again.

Here is the error

'updatedRows': 1, 'updatedColumns': 1, 'updatedCells': 1}}
done
END RequestId: c20b4f94-0b27-4edc-bff6-e411d6d163f1
REPORT RequestId: c20b4f94-0b27-4edc-bff6-e411d6d163f1  Duration: 305675.98 ms  Billed Duration: 305676 ms  Memory Size: 1024 MB    Max Memory Used: 206 MB 
RequestId: c20b4f94-0b27-4edc-bff6-e411d6d163f1 Error: Runtime exited without providing a reason
Runtime.ExitError

Done indicates the program has been completed. I have set the timeout to 15 mins but it doesnt take that long and I dont get a time out error.

Here is the docker code

# Dockerfile, Image, container

FROM python:3.9


COPY . /opt/app
WORKDIR /opt/app
RUN pip install -r ./requirements.txt 

CMD ["python", "./run.py"]

I have checked and I dont call the function anywhere except in run.py. All run.py does is call the function.

from dev_main import runJobs as run

run()

and in dev_main.py I dont call any functions


Solution

  • So I first have to admit I am really disappointed with the response I got from here and the AWS discord. Toxicity in this community can be horrible.

    Anyways. This is a very unique issue and lambda can be really unique in how it works and executes, this article outlines it quite nicely https://docs.aws.amazon.com/lambda/latest/dg/images-create.html

    The main issue was my docker file.

    In the Dockerfile I had

    CMD ["python", "./run.py"]
    

    which would run just fine with lambda, but then lambda would treat it as some sort of error, but not give any good form of feedback.

    The issue is that you need this format

    in run.py I needed this function:

    def lambda_handler(event, context):
        # TODO implement
        run()
    

    and in my Dockerfile I needed

    FROM public.ecr.aws/lambda/python:3.8
    
    # Copy function code
    COPY dev_main.py ${LAMBDA_TASK_ROOT}
    COPY run.py ${LAMBDA_TASK_ROOT}
    
    # Install the function's dependencies using file requirements.txt
    # from your project folder.
    COPY keys.json .
    COPY requirements.txt  .
    RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
    
    # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
    CMD [ "run.lambda_handler" ]