DEV Community

Cover image for Deploying AWS Lambda with Docker: Bypass the 250MB Layer Limit
Olalekan Oladiran
Olalekan Oladiran

Posted on

1 1

Deploying AWS Lambda with Docker: Bypass the 250MB Layer Limit

Introduction

Deploying Lambda functions with large dependencies can be frustrating—especially when you bump into the strict 250MB layer limit. But there’s a workaround: containerized deployment.

In this guide, we will walk through deploying a Lambda function using Docker images, bypassing size restrictions while keeping setup simple. No advanced AWS knowledge required—just a step-by-step approach to get you up and running.

Requirements

  • Docker
  • AWS CLI
  • VS Code

Creating src and main.py

  • Create a folder by running the command

    mkdir [folder-name]

  • cd into the new folder:

    cd [folder-name]

Image description

  • Create source folder(src)in the root folder that will house your python file by running the following command:

mkdir src

and cd into it to create your python file.

  • I named it main.py in my case but it can be any name. Image description We will use a basic Lambda handler for this example, it logs the event/context details and responds with 'Hello world'.
import logging

logging.basicConfig(level=logging.INFO, force=True)
logger = logging.getLogger(__name__)

def handler(event, context):
    logger.info(f"Received event\n{event}")
    logger.info(f"Received context\n{context}")

    return "Hello world"
Enter fullscreen mode Exit fullscreen mode

Creating Dockerfile

  • Next is to create Dockerfile at the root folder by running the command mkdir Dockerfile

We are using AWS's Python base image. For other languages, see AWS's documentation on Lambda base images.

# Step 1: Pull the base image
FROM public.ecr.aws/lambda/python:3.10

# Step 2: Copy requirements.txt
COPY ./requirements.txt ${LAMBDA_TASK_ROOT}

# Step 3: Install the specified packages
RUN pip install -r requirements.txt

# Step 4: Copy function code
COPY ./src ${LAMBDA_TASK_ROOT}

# Step 5: Set the CMD to your handler
CMD [ "main.handler" ]
Enter fullscreen mode Exit fullscreen mode

Image description
Ensure that your file path is accurate.

Image description

Creating Elastic Container Registry

  • Head over to your AWS console to create ECR to house your container image.
  • Search for ECR and select Elastic Container Registry Image description
  • Click create a repository. Image description
  • Choose a name for your repository, leave other settings as default and click create. Image description Image description
  • Open the newly created repository and select view push commands Image description
  • It will open up the command to build and push your image to your ECR. Image description

Build and Push docker image

  • Copy the first command and run it at the root level in VS Code Image description
  • Copy and run the second command to build your docker image Image description
  • Next is to copy the third command to tag the image Image description
  • Finally copy the last command to push the image to ECR Image description
  • You can confirm that the image has been pushed by refreshing your ECR page. Image description

Creating Lambda function

  • We need to create a lambda by searching for lambda function and select lambda. Image description
  • Click create a function Image description
  • Choose container image option, give a name to your function, copy and paster the URI of your image and click create. Image description Image description Image description Image description

Test your function

  • Test your function to see if it is working. Click on the Test tab and select Test. Image description Image description Now you can customize the handler in ./src/main.py and add required dependencies to ./requirements.txt for your specific use case.

Thanks for staying till the end

Warp.dev image

Warp is the #1 coding agent.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!