Posted inAWS

Deploying AWS Lambda with Docker & Serverless Framework

Introduction

AWS Lambda is a powerful serverless compute service, but it comes with limitations on runtimes, package sizes, and dependencies. By using Docker containers, we can overcome these limitations and gain more flexibility.

In this article, we’ll explore:

  • How to deploy an AWS Lambda function with Docker & Serverless Framework.
  • The advantages of using this approach.
  • The downsides to consider.

1. Why Use Docker for AWS Lambda?

By default, AWS Lambda only supports certain runtimes (Python, Node.js, Java, etc.), and each runtime has specific versions. This can be restrictive, especially for applications with large dependencies, custom libraries, or unsupported runtimes.

With Docker, we package everything into a container, allowing us to:
-> Use any programming language (e.g., PHP, Rust, Go).
-> Install custom OS libraries and dependencies.
-> Overcome Lambda’s 50MB deployment limit.
-> Run older Python versions (like 3.6 or 2.7) that AWS no longer supports.

 

2. Setting Up AWS Lambda with Docker Using Serverless Framework.

Step 1: Install Serverless Framework

npm install -g serverless

Step 2: Create a Serverless Project

serverless create --template aws-python-docker --path my-lambda-docker
cd my-lambda-docker

Step 3: Define the Serverless Configuration (serverless.yml)

service: lambda-docker-python

provider: 
   name: aws 
   region: us-east-1 
   ecr: 
     images: 
        python-lambda-image: 
          path: .

functions:
    myFunction:
        image: 
           name: python-lambda-image
        events:
           - http:
               path: hello
               method: get
               cors: true
plugins:
  - serverless-offline

Step 4: Create a Dockerfile

FROM public.ecr.aws/lambda/python:3.9 # Change version if needed

COPY app.py .

CMD ["app.lambda_handler"]

Step 5: Create app.py

import json

def lambda_handler(event, context):
   return {
     "statusCode": 200,
     "body": json.dumps({"message": "Hello from Dockerized AWS Lambda!"}),
   }

Step 7: Invoke the API

curl https://your-api-gateway-url/dev/hello

3. Pros and Cons of AWS Lambda with Docker

Pros (Advantages)

Advantage Description
Supports Any Runtime Run Go, Rust, PHP, or old Python versions that AWS doesn’t natively support.
Overcomes Package Size Limits Normal Lambda deployments (zip files) are limited to 50MB, but Docker images can be up to 10GB.
Custom OS & Libraries Install system-level dependencies (e.g., FFmpeg, OpenCV, TensorFlow) that wouldn’t work in a normal Lambda.
Easier Dependency Management No need to bundle dependencies manually—use a Dockerfile to install them.
Portable & Reusable The same Docker image can run in Lambda, ECS, or Kubernetes, avoiding vendor lock-in.
CI/CD Friendly Dockerized Lambda works smoothly with GitHub Actions, Jenkins, and GitLab CI/CD.
Local Testing with Serverless Offline Run Lambda in a container on your local machine for easy debugging.

Cons (Disadvantages)

Disadvantage Description
Larger Deployment Time Docker images are bigger than zip files, so deployments take longer.
Cold Start Impact Containers may introduce slightly higher cold start times compared to zip-based Lambdas.
More AWS Configuration Needed You need to manage ECR (Elastic Container Registry) permissions and IAM roles.
Docker Complexity If you’re not familiar with Docker, setting up and managing images may be challenging.
Old Versions Can Be Risky Running older Python versions (e.g., 3.6 or 2.7) means no security updates, which can be dangerous.

4. When Should You Use Docker for AWS Lambda?

Use Dockerized Lambda If:

  • You need a custom runtime (e.g., PHP, Rust, or old Python versions).
  • Your app depends on large packages (e.g., Pandas, TensorFlow).
  • You want to deploy the same app to multiple environments (Lambda, ECS, on-prem).
  • Your team already uses Docker for development and CI/CD.

Stick with Standard Lambda If:

  • Your function is small and simple (e.g., handling JSON requests).
  • You want the fastest cold start times (ZIP files are faster than containers).
  • You don’t want to manage Docker, ECR, and IAM roles.

5. Conclusion

Dockerized AWS Lambda with Serverless Framework gives you flexibility, portability, and better dependency management, but it comes with higher deployment times and slightly increased complexity.

If you’re dealing with large dependencies, unsupported runtimes, or advanced CI/CD, this approach is a great solution. But for small, simple functions, standard Lambda with zip deployments is still the best choice.

Final Tip: If you go with Docker, always optimize your images (e.g., use Alpine Linux for smaller containers) to reduce cold start times!

Leave a Reply