Overcoming AWS Lambda Layer Size Limits with Docker

Published on

Overcoming AWS Lambda Layer Size Limits with Docker

AWS Lambda has transformed the way we think about serverless computing. Its ability to run code in response to events without the need to provision servers allows developers to focus on their applications. However, while Lambda offers many benefits, it comes with certain limitations, particularly regarding deployment size. When working with Lambda functions, developers often hit a wall with the 250 MB uncompressed deployment package limit for Lambda layers.

In this post, we will explore how to overcome this limit using Docker. By leveraging Docker, you can package your AWS Lambda functions and their dependencies efficiently, enabling you to utilize the full power of serverless functions without worrying about size constraints.

Understanding AWS Lambda Layers

AWS Lambda Layers allow you to package libraries and dependencies as separate artifacts. This encourages code reusability and simplifies the deployment of functions. However, even with layers, the total size (including layers and the function itself) cannot exceed 250 MB uncompressed. This limitation can be problematic for functions with large dependencies or multiple layers.

Why Use Docker?

Docker simplifies the process of packaging and deploying applications. Using Docker, you can create reproducible environments that simulate the Lambda execution environment locally. The key benefits of using Docker with AWS Lambda include:

  1. Simplicity in Dependency Management: It allows you to include all necessary binaries and dependencies without worrying about space constraints.
  2. Testing Locally: You can replicate the Lambda environment on your local machine to test your functions thoroughly before deploying.
  3. Granular Control: Docker lets you customize your execution environment as per your application's requirements.

Creating a Lambda Function with Docker

Step 1: Set up your Docker environment

First, ensure that you have Docker installed on your local machine. Once Docker is installed, you can create a new directory for your project:

mkdir lambda-docker-example
cd lambda-docker-example

Step 2: Create a Dockerfile

In this directory, create a file named Dockerfile. This file defines our Lambda environment using Docker. Below is a simple example of a Dockerfile that sets up a Node.js Lambda function:

# Use the official AWS Lambda Node.js image
FROM public.ecr.aws/lambda/nodejs:14

# Copy the current directory contents into the container
COPY . ${LAMBDA_TASK_ROOT}

# Install any dependencies if necessary
# RUN npm install

# Command to run the Lambda function
CMD ["index.handler"]

Commentary:

  • FROM: This line specifies the base image. Here we're using the AWS Lambda Node.js runtime.
  • COPY: This command copies your function code into the container.
  • RUN: This command can be used to install additional libraries. The commented-out install command can be uncommented when using a package manager like npm.

Step 3: Create Your Function Code

Next, create a file named index.js in the same directory:

exports.handler = async (event) => {
    console.log('Event: ', event);
    return {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda with Docker!'),
    };
};

Commentary:

  • The above code is a simple Lambda function that logs the event it receives and returns a "Hello" message as a response.

Step 4: Build Your Docker Image

Now let's build your Docker image. In the terminal, run:

docker build -t my-lambda-function .

Commentary:

  • This command builds a Docker image named my-lambda-function based on the context of our current directory (.).

Step 5: Test Your Lambda Function Locally

You can simulate invoking your Lambda function by using the following command:

docker run -p 9000:8080 my-lambda-function

After running this command, you should be able to access your function locally by navigating to http://localhost:9000/2015-03-31/functions/function/invocations in your web browser or using a tool like curl:

curl -X POST http://localhost:9000/2015-03-31/functions/function/invocations -d '{}'

Commentary:

  • The docker run command exposes port 9000 on your local machine and maps it to port 8080 within the container, allowing you to invoke your function.

Step 6: Push Your Image to Amazon ECR

With your function working as expected locally, the next step is to upload your image to the AWS Elastic Container Registry (ECR). First, ensure you have the AWS CLI installed and configured. You can find instructions here.

  1. Create a repository in ECR using the console or the CLI:
aws ecr create-repository --repository-name my-lambda-repo
  1. Authenticate Docker to your ECR registry:
aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com
  1. Tag your image for the repository:
docker tag my-lambda-function:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-lambda-repo:latest
  1. Push the Docker image to ECR:
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-lambda-repo:latest

Step 7: Create Your Lambda Function

Now you can create a new Lambda function in the AWS Management Console:

  1. Open the Lambda console.
  2. Click on "Create function".
  3. Select "Container image".
  4. Enter the URI of your ECR image and configure any necessary permissions.

Step 8: Test Your Lambda Function in AWS

Once the function is created, you can invoke it via the AWS console, SDK, or CLI. To test it via the console, navigate to your function and select "Test". Create a new test event and execute it.

Bringing It All Together: Embracing Limitless Possibilities

By wrapping your Lambda functions in Docker, you can efficiently handle the limitations that AWS imposes on Lambda layers. With Docker, you're free to include as many dependencies as you need, and you can also create more complex Lambda functions that can perform a multitude of tasks without being constrained by layer size limits.

For a deeper dive into using AWS Lambda with Docker, you can check the official AWS documentation on Custom Lambda Runtimes.

As you embark on your serverless journey, don't let size limits restrict your creativity. Embrace the full power of your applications through Docker, and redefine what’s possible in the world of AWS Lambda and serverless computing.