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.
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
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:
Create a file named app.py
in your project directory:
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'Hello from local Lambda!'
}
AWS provides base images for each runtime. We'll clone the repo and build the one for Python 3.10.
git clone https://github.com/aws/aws-lambda-base-images
cd aws-lambda-base-images
git checkout python3.10
cd x86_64
docker build -t lambda-python-3.10:local -f Dockerfile.python3.10 .
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"]
This tells Docker to use your custom Lambda base image, copy in your Lambda function, and specify the handler.
docker build -t lambda-app .
docker run -p 9000:8080 lambda-app
This exposes port 9000 so you can invoke the 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!"
}
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! π¬
π AWS Lambda Base Images (GitHub)