How to build and package Node.js Lambda code along with dependencies in Docker

Recently I was working on my wife’s personal website and needed to implement AWS Lambda@Edge that resizes and compresses the requested image before returning to the users in a way similar to Resizing Images with Amazon CloudFront & Lambda@Edge (AWS CDN Blog) but faced an issue with the Node.js sharp package that heavily relies on the OS libraries so prevents compilation of the JavaScript code in a straightforward way.

I found the approach to build the code described in the AWS blog post quite sophisticated and not really reusable across different projects, searching through the internet also didn’t provide me with convenient options, so I decided to create a GitHub template repository that has all the needed operations encapsulated in a single command:

npm run package

And this is a short post explaining how it works so everyone can use it.

Important to understand that this approach enables the compilation of the Node.js code with any libraries and runtime dependencies needed within the Docker container without using the Docker in AWS Lambda at all. The result is a zip archive that can be easily uploaded to AWS.


The BASE image is based on the official Amazon Linux image to have the most similar setup to what will be used to run the Lambda code.

First, only needed OS packages installed as well as nvm to have the actual Node.js runtime version — the one, that you configured for the AWS Lambda.

Next, in the BUILD image, the dependencies are installed and npm run build command executed to compile the code.

After that, the APPLICATION image installs runtime dependencies only and gets the artifact from the BUILD image (dist directory).

Eventually, all the code in the APPLICATION image, including node_modules the directory (containing runtime dependencies only), is being packaged into the archive.

Unfortunately, I haven’t found a working way to avoid source ~/.bashrc before when triggering npm scripts in this Dockerfile.

npm scripts

Following npm scripts are configured to do the work:

  • docker:build to build the image;
  • docker:create to create a temporary container without starting it;
  • docker:copy copies the archive from the container;
  • docker:clean removes the temporary container;
  • package runs commands above in a sequence.

That’s all Folks!

GitHub template repository:




Solution Architect, IoT hobbyist, petrolhead

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Java programming language

Casino geant annemasse

Quantum Approximate Optimization Algorithms on the “Traveling Salesman Problem”

Akka Streams — Getting Started

Good night gaga8b all the best 1..💞

Regex (Regular Expressions) Demystified

Git patch apply and remove

Gitlab — How to pass files/variables between Jobs using artifact.

Job A

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danila Loginov

Danila Loginov

Solution Architect, IoT hobbyist, petrolhead

More from Medium

Deploy a NodeJs Application to AWS Lambda using the Serverless framework


Introducing alanajs: AWS Lambda and API Gateway, Simplified for JavaScript

Frictionless Serverless Development: Part 3— Authentication

Create a Gitlab CI/CD pipeline for a serverless Node.js application on AWS