Home > Net >  AWS Lambda Docker Image
AWS Lambda Docker Image

Time:07-20

I am attempting to deploy a AWS Lambda function with the docker image run time.

I have followed the instructions here.

The only difference is that my code resides in a src folder with other files and modules.

So I changed the Dockerfile CMD command to

CMD [ "src.main.handler" ]

But the AWS env does not seem to like that. I can find many examples of deploying single file applications but none about applications with an entrypoint in a folder.

Has anyone had any success with this before?

I should note that this application is not an api, it is aimed to do some processing and write to s3, so the docs where you can call the image locally through a port seem useless.

my dockerfile is

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

COPY            src ${LAMBDA_TASK_ROOT}
COPY            requirements.txt ${LAMBDA_TASK_ROOT}

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

WORKDIR         ${LAMBDA_TASK_ROOT}

CMD             [ "src.main.handler" ]             

My file tree is as follows:

.
├── deploy
│   ├── backtesting
│   │   └── Dockerfile
├── src
│   ├── __init__.py
│   ├── aws
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   └── ssm.cpython-39.pyc
│   │   └── ssm.py
│   ├── db
│   │   ├── __init__.py
│   │   │   └── database_connector.cpython-39.pyc
│   │   └── database_connector.py
│   ├── main.py
│   ├── streams
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── athena_stream.cpython-39.pyc
│   │   │   ├── database_stream.cpython-39.pyc
│   │   │   ├── model_stream.cpython-39.pyc
│   │   │   └── owned_horse_stream.cpython-39.pyc
│   │   ├── athena_stream.py
│   │   ├── database_stream.py
│   │   ├── manual_trigger_stream.py
│   │   └── owned_horse_stream.py
│   ├── utils
│   │   ├── helpers.py
│   │   ├── market_book_filters.py
│   │   └── pricing_helpers.py

This is the error I get when testing locally

{"errorMessage": "Unable to import module 'src.main': No module named 'src'", "errorType": "Runtime.ImportModuleError", "stackTrace": []}```

CodePudding user response:

If you put more details in the question I can help you. How is your structure tree? What's inside your Dockerfile? How are you deploying your lambda to AWS?

Anyway your error is pretty explicit. And yes, you can execute your lambda locally with any AWS event you wish.

(I would have commented but my reputation does not allow me to do so.)

CodePudding user response:

The answer was solved by @thesynde mentioning that I was infact importing all the files inside of my src folder and not the folder itself.

This was fixed by changing the copy command to

COPY . ${LAMBDA_TASK_ROOT}

and it working.

  • Related