Oliver Nassar

Build NodeJS or NPM module for AWS Lambda (as a layer)

Below you'll find a quick code example of how to convert a NodeJS or NPM module into an AWS Lambda Layer. It's using version 1.14.6 of the follow-redirects package.

cd ~/
mkdir tmp/
mkdir tmp/follow-redirects
cd tmp/follow-redirects
npm init -y
npm install --arch=x64 --platform=linux follow-redirects
zip -r follow-redirects-v1.14.6.zip node_modules/ package.json package-lock.json
mv follow-redirects-v1.14.6.zip ~/
cd ~/
rm -rf tmp/follow-redirects
ls ~/follow-redirects-v1.14.6.zip

The zip call is super important. I made the mistake (for hours) of trying to zip up the whole parent directory, but for some reason AWS Lambda Layers don't like that. I realized that I needed to zip up the individual files.

In order to use the follow-redirects module, you add the Layer to your AWS Lambda Function, and then use it with the following line of code:

const {http, https} = require('/opt/node_modules/follow-redirects');

Acknowledgments