So I'm often making serverless Python programs on AWS Lambda, and often I need to add dependencies. You can add these using a zip file or a Dockerfile (see aws documentation). Now I think a zip file works just fine, but a Dockerfile is of course a much newer and more advanced technology. However I'm can't really think of any advantages that it offers for this specific usecase.
Which do you prefer and why?
Example zip pipeline:
pip install --target ./package requests boto3 bs4
cd package; zip -r ../stonksoverflow.zip .
cd ..; zip -g stonksoverflow.zip lambda_function.py
aws lambda create-function --function-name lambda_function --zip-file fileb://stonksoverflow.zip --handler lambda_function.lambda_handler --runtime python3.8 --role arn:aws:iam::123456789:role/stonksoverflow
Example Docker pipeline:
FROM public.ecr.aws/lambda/python:3.8
RUN pip3 install requests bs4 boto3
COPY lambda_function.py ${LAMBDA_TASK_ROOT}
CMD [ "stonksoverflow.handler" ]
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 123456789.dkr.ecr.eu-west-1.amazonaws.com
docker tag stonksoverflow:latest 331730032056.dkr.ecr.eu-west-1.amazonaws.com/stonksoverflow:latest
docker push 123456789.dkr.ecr.eu-west-1.amazonaws.com/wstonksoverflow:latest
The Docker approach to Lambda management has two main advantages over the .zip approach:
With the .zip approach, if one of your dependencies or sub-dependencies is written in C (e.g., NumPy, lxml), it must be built on an environment "similar to the environment in AWS Lambda (Amazon Linux)". Docker allows you to build any dependency in an Amazon Linux environment, ensuring that it will build remotely. This is great because you avoid the tedious debugging process of uploading libraries one at a time and hoping they import successfully.
Because Lambda runs on Amazon Linux, running your code in a Docker container with the Amazon Linux image enables you to test your code as if it were running in Lambda. You no longer have to hope that when your code runs in Lambda it will behave the same way it did locally-- you'll know before you push it.
Here's a couple articles I found useful:
https://dashbird.io/blog/deploying-aws-lambda-with-docker/ https://medium.com/@johnnyopao/python-dependencies-and-aws-lambda-18acbdebca20
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With