Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikcorg/376e35614e1ff1087c8c to your computer and use it in GitHub Desktop.
Save nikcorg/376e35614e1ff1087c8c to your computer and use it in GitHub Desktop.
Compile a self-contained bundle for use in AWS Lambda
#!/usr/bin/env bash
ERR_BROWSERIFY_NOT_FOUND=1
ERR_SOURCE_NOT_FOUND=2
AWS_STDLIBS="aws-sdk dynamodb-doc"
err_exit()
{
if [ "x$2" != "x" ]; then
echo "Error: $2" >&2
fi
exit $1
}
compile_omit_list()
{
local IFS=" "
local OMIT_PARAMS
for LIB in $AWS_STDLIBS; do
OMIT_PARAMS="$OMIT_PARAMS -u $LIB"
done
echo $OMIT_PARAMS
}
if [ "x$(which browserify)" = "x" ]; then
err_exit $ERR_BROWSERIFY_NOT_FOUND "browserify not found"
fi
SOURCE="index.js"
if [ "x$1" != "x" ]; then
SOURCE="$1"
fi
if [ ! -f "$SOURCE" ]; then
err_exit $ERR_SOURCE_NOT_FOUND "source file not found: $SOURCE"
fi
TARGET="bundle-$SOURCE"
if [ "x$2" != "x" ]; then
TARGET="$2"
fi
browserify --bare --no-bf --standalone bundle $(compile_omit_list) -e "$SOURCE" > "$TARGET"
@nikcorg
Copy link
Author

nikcorg commented Jul 22, 2015

As long as not using native modules, this approach should work ok. For bundling with native modules, see: https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

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