I am building an app using AWS lambda function. My own code logic is very simple: one python file calling some ML functions. On the contrary, it requires a lot of packages (dependencies). I know for smaller cases I can zip these dependencies and upload the zip file for the lambda function. However, it doesn't fit my use case as the zip file can easily exceed the 250 MB size limit. So I have to use the Docker image approach - create a lambda function directly from the Docker image.
I am using sam tool to build / deploy changes. Deploy is very time consuming as it needs to push a very big (6G) image to ECR. The worst thing is I have to endure this every time even making a small changes on my own code and never touching the dependencies.
Can I apply the same way as zip approach, i.e, only include dependencies in the docker image and put customized logic out of it? Is it doable?
If 1 is not possible, what is the best practice / tips for this? I guess there are some magic in Dockerfile but I am pretty noob here. Any demo / sample codes would be great. I linked my dockerfile below:
FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.9
RUN python3.9 -m pip install --upgrade pip
COPY requirements.txt ./
RUN python3.9 -m pip install -r requirements.txt -t .
COPY *.py ./
# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]
I found some suggestions to upload the package to S3 and download it from S3 into tmp folder. This doesn't sound very cool and even if it is, there is also a 512 MB limit which still too small for my use case.
Update 1
I want to add more context about the app. The app is a prototype to run stable diffusion model. The image is big because it requires bunch of ML packages.
Here is my requirements.txt
numpy==1.19.5
opencv-python==4.5.5.64
transformers==4.16.2
diffusers==0.2.4
tqdm==4.64.0
openvino==2022.3.0
huggingface_hub==0.9.0
scipy==1.9.0
streamlit==1.12.0
watchdog==2.1.9
ftfy==6.1.1
replicate
uvicorn
requests
fastapi
mangum
I would say the application is pretty similar to this project and the dockerfile is also similar.
One tool I found pretty handy is sam cli. When code changes, everytime I just need to run sam build
(finish in seconds) and sam deploy
(<5 minutes), which greatly reduces my pain. Also, it has a bootstrap functionality which could set up automatically based on the choice of using zip or image approach.