Our Blog

How to Deploy AWS Lambda over 250MB in Size with Serverless

How to deploy aws lambda

AWS Lambda is a great serverless solution by itself and together with Serverless, it is easy to develop and deploy cloud applications. Lambda is great but not perfect as it comes with its own set of limitations. One of which is size – the total size of the deployed unzipped package cannot exceed 250MB. 

In most cases, this is not an issue, however, what if you need to have all those libraries and there is no way around it? You have probably seen a message similar to this when the size of the package you were trying to deploy exceeds the limit:

An error occurred (InvalidParameterValueException) when calling the UpdateFunctionCode operation: Unzipped size must be smaller than 262144000 bytes

One of the options would be to switch to a setup with a server (e.g. EC2, ECS, etc), but then you’d lose all the benefits of the serverless design not to mention added massive complexity into the deployment.

Luckily, in Feb 2021 Amazon introduced Lambda on Image which optionally allows a lambda to have its code sourced from docker image in ECR. So we don’t have to make that difficult choice anymore.

First, we need to prepare a Dockerfile with a container image. To be usable by Lambda the image must follow a certain structure. For this example, we’ll use one of the available AWS Base images.

FROM public.ecr.aws/lambda/python:3.8
ARG handler
ARG requirements

RUN echo "$handler" > handler.py
RUN echo "$requirements" > requirements.txt
RUN /var/lang/bin/python3.8 -m pip install --upgrade pip
RUN pip install -r requirements.txt

CMD [ "handler.main" ]

Next, we need to create the repo if it doesn’t exist

aws ecr describe-repositories --repository-names lambda_repo --region us-east-2 || aws ecr create-repository --repository-name lambda_repo --region us-east-2 --image-scanning-configuration scanOnPush=true

Now, we need to build the image

docker build --no-cache -t lambda_repo
--build-arg handler=”$(cat handler.py)”
--build-arg requirements=”$(cat requirements.txt)”

Once done, we push the image to ECR

docker tag lambda_repo:latest ${ACCOUNT}.dkr.ecr.us-east-2.amazonaws.com/lambda_repo:latest
aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin ${ACCOUNT}.dkr.ecr.us-east-2.amazonaws.com
docker push ${ACCOUNT}.dkr.ecr.us-east-2.amazonaws.com/lambda_repo:latest

Now it’s time to create lambda with our image. To do that follow your normal routine with the exception of not uploading Lambda code directly and using an image instead.

Click “Create Function” and voi-la!

Share this with your friends

One thought on “How to Deploy AWS Lambda over 250MB in Size with Serverless

Leave a Reply

Your email address will not be published. Required fields are marked *