Skip to content

Instantly share code, notes, and snippets.

@mridang
Last active October 15, 2024 06:03
Show Gist options
  • Save mridang/4d87a4d8813d5c54b3356626ee02906e to your computer and use it in GitHub Desktop.
Save mridang/4d87a4d8813d5c54b3356626ee02906e to your computer and use it in GitHub Desktop.
Publing NPM modules as Lambda layers

I recently needed to use a Node-based Lambda where it depended on an NPM module.

https://github.com/mridang/serverless-servestatic-plugin/blob/a9e8f3c677e60be2065e84ded8cc5fe1da72e813/src/lambda.js#L7-L10

I didn't wan't to begin using a bundler, switch runtimes or do any workarounds. I just needed to get this custom resource up and running.

Note

I'm not sure whether this whole approach is overkill and could be better accomplished by using URL-based imports. At the time of writing, I haven't seen a URL-based imports except in Deno.

The following script installs the NPM packages into a directory called nodejs, packs it into an archive and then uploads it. The script is rather crude and publishes a new version of the layer on every invocation.

#!/bin/bash

# Variables
LAYER_NAME="adm-zip-layer"
RUNTIME="nodejs20.x"  # Change this to your Lambda's runtime
TEMP_DIR="adm-zip-layer"
ZIP_FILE="adm-zip-layer.zip"

# Step 1: Create directory structure
mkdir -p $TEMP_DIR/nodejs

# Step 2: Install adm-zip and dependencies into the nodejs folder
npm install adm-zip --prefix $TEMP_DIR/nodejs
npm install minimatch --prefix $TEMP_DIR/nodejs
npm install mime-types --prefix $TEMP_DIR/nodejs

# Step 3: Package the layer
cd $TEMP_DIR
zip -r ../$ZIP_FILE nodejs
cd ..

# Step 4: Publish the layer to AWS
aws lambda publish-layer-version --layer-name $LAYER_NAME \
  --zip-file fileb://$ZIP_FILE \
  --compatible-runtimes $RUNTIME

# Cleanup
rm -rf $TEMP_DIR
rm $ZIP_FILE

echo "Lambda layer $LAYER_NAME published successfully."
@mridang
Copy link
Author

mridang commented Oct 9, 2024

I just wanted to sanity check if this in fact is possible so I just queried ChatGPT. https://chatgpt.com/share/67062a1f-ae70-8006-80c6-0d754f3720f3

I've come to the conclusion that packing additional dependencies is the correct way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment