Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function not able to find other packages in same directory

I am deploying a lambda function as a container image. Here's my project structure :

  • core
  • plugins
  • lambda_handler.py

All three are at the same level - /var/task

Inside lambda_handler.py I am importing the core package, but when I test it locally it says :

"errorMessage": "Unable to import module 'lambda_handler': No module named 'core'"

Dockerfile

FROM public.ecr.aws/lambda/python:3.9

# Copy requirements to container
COPY requirements.txt .

# install dependencies
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

# Copy app folders to container
COPY core ${LAMBDA_TASK_ROOT}
COPY plugins ${LAMBDA_TASK_ROOT}
COPY lambda_handler.py ${LAMBDA_TASK_ROOT}
COPY __init__.py ${LAMBDA_TASK_ROOT}

CMD ["lambda_handler.lambda_fun"]

lambda_handler.py

import json

from core.nlp.service import nlp_service


def lambda_fun(event, context):
    return json.dumps(nlp_service.get_ner())

requirements.txt

pyspark==3.1.2
spacy
pymupdf
boto3
cloudpathlib
spark-nlp==3.4.1
numpy

like image 893
Rishabhg Avatar asked Oct 30 '25 06:10

Rishabhg


1 Answers

If you just use

COPY core ${LAMBDA_TASK_ROOT}

it will copy the content of the core into ${LAMBDA_TASK_ROOT}. To copy into core folder, it should be:

COPY core ${LAMBDA_TASK_ROOT}/core
like image 81
Marcin Avatar answered Nov 01 '25 19:11

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!