aws / aws-lambda-base-images

Apache License 2.0
648 stars 107 forks source link

Need documentation for python 3.9 #35

Open SeamusY opened 2 years ago

SeamusY commented 2 years ago

I need some documentation on how to combine the docker file for python 3.9 to contain my custom library + requirements.txt for installing all the files.

I am using the library below, please provide any suggestion.

https://docs.python.org/3/library/venv.html

carlzogh commented 2 years ago

Hey @SeamusY thanks for reaching out - please refer to the AWS documentation on Creating a Python image from an AWS base image.

For python3.9 you can use a Dockerfile similar to the below:

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

# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}

# Install the function's dependencies using file requirements.txt
# from your project folder.
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 [ "app.handler" ]
SeamusY commented 2 years ago

I did however the bootstrap is causing issues, ill copy the error of the logs but it has a “Sanity_check” trigger problem in combined with my libraries.

Yours Faithfully, SeamusY

On 20 Jan 2022, at 02:48, Carl Zogheib @.***> wrote:

 Closed #35.

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you were mentioned.

SeamusY commented 2 years ago

Secondly it doesn’t answer the issue of the venv, which is what i wanted to request as i am adding libraries. Unless you recommend not to follow such standards?

Yours Faithfully, S

On 20 Jan 2022, at 02:58, Seamus Yeo @.***> wrote:

I did however the bootstrap is causing issues, ill copy the error of the logs but it has a “Sanity_check” trigger problem in combined with my libraries.

Yours Faithfully, SeamusY

On 20 Jan 2022, at 02:48, Carl Zogheib @.***> wrote:

 Closed #35.

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you were mentioned.

carlzogh commented 2 years ago

@SeamusY could you please share your Dockerfile and the error messages you're seeing?

SeamusY commented 2 years ago

Docker:

# FROM public.ecr.aws/serverless/extensions/lambda-insights:12 AS lambda-insights
FROM public.ecr.aws/lambda/python:3.9

ENV LAMBDA_TASK_ROOT=/var/task
ENV VIRTUAL_ENV=/opt/venv

RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY requirements.txt .
COPY scan.py ${LAMBDA_TASK_ROOT}
ADD pyimagesearch ${LAMBDA_TASK_ROOT}/pyimagesearch

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

CMD [ "/var/task/scan.lambda_handler" ]

Error (Only happens during run time):

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
{"errorMessage": "Empty module name", "errorType": "ValueError", "requestId": "", "stackTrace": ["  File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n    return _bootstrap._gcd_import(name[level:], package, level)\n", "  File \"<frozen importlib._bootstrap>\", line 1027, in _gcd_import\n", "  File \"<frozen importlib._bootstrap>\", line 961, in _sanity_check\n"]}%  

@carlzogh

kuakman commented 5 months ago

I'd also like to add a comment to clarify the scope of these docker images that apparently don't behave the same as the one you could easily configure/spin up from the aws console. For example: query string parameters are not supported. I couldn't find any related documentation so far.

if you do attempt to pass parameters by query string, you get a nasty error:

Traceback (most recent call last):able to unmarshal input: Expecting value: line 1 column 1 (char 0)

Example: http://[localhost]/2015-03-31/functions/function/invocations?param1=value1&paramN=valueN

whereas, in a traditional python based lambda, this is possible by capturing them via the event['queryStringParameters'] prop.

I wish there was a more clear documentation about what's exposed somewhere in here: https://gallery.ecr.aws/lambda/python