The below one is my application directory. There is lambda_handler
function on the main.py
file.
.
├── Dockerfile
└── app
└── src
└── main.py
The below one is my Dockerfile
.
FROM public.ecr.aws/lambda/python:3.9
WORKDIR /app
COPY /app /app
RUN yum -y install gcc libmariadb-dev default-libmysqlclient-dev python-devel mysql-devel
RUN /var/lang/bin/python3.9 -m pip install --upgrade pip
RUN pip install -U poetry
RUN poetry export --only main --without-hashes --format=requirements.txt > requirements.txt
RUN pip install -r requirements.txt -t .
ENV PYTHONPATH="$PYTHONPATH:${LAMBDA_TASK_ROOT}"
WORKDIR ${LAMBDA_TASK_ROOT}/src
ENTRYPOINT ["/lambda-entrypoint.sh"]
CMD ["main.lambda_handler"]
I push my image to ECR with GitHub Actions and use it on Lambda. However, when I trigger the function, it raises module not found error.
{
"errorMessage": "Unable to import module 'main': No module named 'main'",
"errorType": "Runtime.ImportModuleError",
"requestId": "...",
"stackTrace": []
}
Additionally, I use other packages from src
modules like the one below.
from typing import Any
from src.custom import Function
def lambda_handler(event: dict[str, Any], context: Any):
Function...
How should I write the Dockerfile
to work Lambda?
I think you are having problems with the paths in the Dockerfile
FROM public.ecr.aws/lambda/python:3.9
COPY app/ ${LAMBDA_TASK_ROOT}
RUN yum -y install gcc libmariadb-dev default-libmysqlclient-dev python-devel mysql-devel
RUN /var/lang/bin/python3.9 -m pip install --upgrade pip
RUN pip install -U poetry
RUN poetry export --only main --without-hashes --format=requirements.txt > requirements.txt
RUN pip install -r requirements.txt -t ${LAMBDA_TASK_ROOT}
ENV PYTHONPATH="$PYTHONPATH:${LAMBDA_TASK_ROOT}"
WORKDIR ${LAMBDA_TASK_ROOT}/src
CMD ["main.lambda_handler"]
I'm not sure about the RUN steps but I think that would solve the not found module "main" issue