Skip to content
Go back

Run AWS Lambda Locally with Docker

Edit page

Run AWS Lambda Locally with Docker

This guide walks you through running an AWS Lambda function written in Node.js using Docker, simulating AWS’s environment.

Step 1: Create a Dockerfile

FROM amazon/aws-lambda-nodejs:14

COPY . ${LAMBDA_TASK_ROOT}
RUN npm install

CMD ["index.handler"]

Replace index.handler with your actual handler filename and function.

Step 2: Build the Docker Image

docker build -t my-lambda-function .

Step 3: Run the Lambda Function

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

You can test the Lambda with a curl request:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"key": "value"}'

Step 4: Simulate Memory and Storage Limits

docker run -p 9000:8080 --memory 512m --tmpfs /tmp:rw,size=512m my-lambda-function

Option: Use Docker Compose

version: "3.8"
services:
  lambda:
    image: my-lambda-function
    ports:
      - "9000:8080"
    deploy:
      resources:
        limits:
          memory: 512m
    tmpfs:
      - /tmp:size=512m

Uploading Function Code

Ensure your Dockerfile contains:

COPY . ${LAMBDA_TASK_ROOT}

Then build the image:

docker build -t my-lambda-function .
docker run -p 9000:8080 -v $(pwd):${LAMBDA_TASK_ROOT} my-lambda-function

This mounts your local code so changes are reflected without rebuilding.


Edit page
Share this post on:

Previous Post
Running and Testing GitHub Actions Locally and in CI
Next Post
Running and Testing GitHub Actions Locally and in CI