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.

Dockerfile

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 function.zip 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 function.zip archive from the container;
  • docker:clean removes the temporary container;
  • package runs commands above in a sequence.

That’s all Folks!

GitHub template repository: https://github.com/loginov-rocks/Build-Lambda-in-Docker

Solution Architect, IoT hobbyist, petrolhead https://loginov.rocks

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

Recommended from Medium

How BOCS helped Central Railway in completing a new line project, 21 days ahead of target date?

ClickHelp Sneak Peeks — Navigation, Taxonomies, mini-TOC, FTS

My Simple Flutter App: Part 1

Stylish yet simple GUI for Python that users, backend developers and data scientists would…

Building Coupon Reminders — Part 2 Serverless and Secured Image Storage and Processing

What is JAMstack, and what can it do for you?

Topic-based Authoring vs. Modular Authoring

Edit your videos AUTOMATICALLY with this Python code!

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 https://loginov.rocks

More from Medium

AWS Lambda Node.js 使用记录

Unit Testing of AWS Lambda Functions (Node.js) using Mocha and Chai

Build a WebSocket Connection with AWS Lambda in React and Ruby

Running a NodeJS app with Postgres in Dokku