Skip to content

Running AWS Lambda functions locally can dramatically improve your development speed and reduce deployment errors. AWS provides official Docker images that replicate the Lambda execution environment so you can test functions on your machine before pushing to the cloud.

Notifications You must be signed in to change notification settings

Pradeeparul2/run-aws-lambda-locally

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 

Repository files navigation

🐳 How to Run AWS Lambda Locally Using Docker

Running AWS Lambda functions locally can dramatically improve your development speed and reduce deployment errors. AWS provides official Docker images that replicate the Lambda execution environment so you can test functions on your machine before pushing to the cloud.

In this guide, you'll learn how to build and run a Lambda function locally using Docker, step by step, using Python 3.10.

Untitled design (1)


πŸš€ Why Run Lambda Locally?

Running Lambda locally allows you to:

  • πŸ” Speed up development with faster feedback loops
  • 🐞 Debug more easily with full access to logs
  • πŸ§ͺ Test without deploying to AWS every time
  • πŸ›‘οΈ Reduce production bugs by matching the AWS runtime

πŸ”§ Prerequisites

Ensure the following tools are installed:

  • Docker
  • Git
  • Basic knowledge of AWS Lambda
  • A Lambda function written in a supported runtime (we'll use Python 3.10)

πŸ“š Useful Resources:


πŸ“ Step 1: Create Your Lambda Function

Create a file named app.py in your project directory:

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from local Lambda!'
    }

1

πŸ™ Step 2: Clone and Build AWS Lambda Base Image

AWS provides base images for each runtime. We'll clone the repo and build the one for Python 3.10.

πŸ‘‰ Clone the Repo

git clone https://github.com/aws/aws-lambda-base-images
cd aws-lambda-base-images

4

πŸ‘‰ Checkout Python 3.10 Branch and Navigate to Architecture Directory (x86_64)

git checkout python3.10
cd x86_64

6

πŸ‘‰ Build the Lambda Python 3.10 Base Image

docker build -t lambda-python-3.10:local -f Dockerfile.python3.10 .

7

πŸ› οΈ Step 3: Create Dockerfile for Your Lambda App

In your project root (outside the cloned repo), create a file named Dockerfile:

FROM lambda-python-3.10:local
COPY app.py ${LAMBDA_TASK_ROOT}
CMD ["app.lambda_handler"]

8

This tells Docker to use your custom Lambda base image, copy in your Lambda function, and specify the handler.

🧱 Step 4: Build and Run Your Lambda Docker Container

πŸ‘‰ Build the Lambda App Image

docker build -t lambda-app .

9

πŸ‘‰ Run the Container

docker run -p 9000:8080 lambda-app

10

This exposes port 9000 so you can invoke the function locally.

⚑ Step 5: Invoke the Lambda Function Locally

Use curl to simulate an event trigger to your function:

curl --location 'http://localhost:9000/2015-03-31/functions/function/invocations' \
--header 'Content-Type: application/json' \
--data '{"payload":"hello world!"}'

βœ… Expected Output:

{
  "statusCode": 200,
  "body": "Hello from local Lambda!"
}

13

βœ… Final Thoughts

Running AWS Lambda functions locally using Docker helps you:

  • Develop faster
  • Debug in real-time
  • Avoid cloud costs for every test

Would you like to explore more?

  • Add a Lambda Layer for shared dependencies?
  • Mount volumes for reading/writing files locally?
  • Connect your Lambda to databases or external APIs?

Let me know or submit an issueβ€”I'd love to write a follow-up! πŸ’¬

πŸ“š Resources

πŸ”— AWS Lambda Base Images (GitHub)

πŸ”— AWS Lambda Python Runtimes (ECR Gallery)

πŸ“˜ AWS Lambda Developer Guide

About

Running AWS Lambda functions locally can dramatically improve your development speed and reduce deployment errors. AWS provides official Docker images that replicate the Lambda execution environment so you can test functions on your machine before pushing to the cloud.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published