Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I build a single js file for AWS Lambda nodejs runtime

We are working on a project/framework that aids in deploying and maintaining code in AWS Lambda. I want to build/bundle all node.js code for a lambda function into one js file because:

  • Smaller codebases help with lambda cold start issues
  • Lambda has code zip size limit of 50MB

We don't want to create a custom bundler to do this because there are many options already out there (systemjs,browserify,webpack etc). HOWEVER we are concerned with issues with some node modules not playing well with bundlers/builders.

Specifically aws-sdk has known issues with webpack, says it has browserify support but I've talked to people who have had issues with browserify'ing aws-sdk

We want to pick an existing bundler (or 2), but we want to make sure it works for as many modules/code as possible. We are trying to create a plugin ecosystem for JAWS, so getting this right is important (don't want to turn people off because module X they use wont bundle).

Questions:

  1. How would I go about bundling/building to satisfy these constraints?
  2. Is there any guidelines we can give to consumers of our product to ensure the code they write/use will bundle? Ex: Dynamic require()s cause issues AFAIK.
like image 965
rynop Avatar asked Aug 27 '15 15:08

rynop


People also ask

What kind of package can you use with node js for Lambda?

You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and . zip file archives. To create the deployment package for a .

Does AWS Lambda support node JS?

You can now develop AWS Lambda functions using the Node. js 16 runtime. This version is in active LTS status and considered ready for general use.

How to update AWS Lambda NodeJS function with dependencies?

In order to update a AWS Lambda Nodejs function with dependencies, follow these steps: Step 1: Open a terminal or shell with the command line. Make sure your local Nodejs version matches the Nodejs version of your function. Step 2: For the deployment package, make a folder. Assume the folder is called my-function in the steps that follow.

How does the NodeJS function runtime work with Lambda?

The Node.js function runtime gets invocation events from Lambda and passes them to the handler. In the function configuration, the handler value is index.handler . Each time you save your function code, the Lambda console creates a deployment package, which is a .zip file archive that contains your function code.

How do I run a NodeJS function in AWS?

For instructions, see Using Packages and Native nodejs Modules in AWS in the AWS compute blog. Open a command line terminal or shell. Ensure that the Node.js version in your local environment matches the Node.js version of your function.

How do I upload a lambda function to AWS Lambda?

1 In the Lambda console, choose your function. 2 Under Function code, for Code entry type, choose Upload a .zip file. 3 Under Function package, choose Upload. 4 Choose the .zip file you created, and then choose Open. 5 At the top of the console, choose Save. ... 6 After uploading is finished, choose Test.


2 Answers

I prefer not to use single file solution, but to upload zip file with all my code packed. I use gulp for this. The code below only uploads production dependencies, excluding development ones and zip archive from previous upload. You may also exclude your test folder or any other file using parameters of gulp.src.

Just to mention, aws-sdk library is in my development dependencies as AWS Lambda already has a copy ;)

var gulp = require('gulp');
var lambda = require('gulp-awslambda');
var zip = require('gulp-zip');
var settings = require('./settings');
var argv = require('yargs').argv;
var p = require('./package.json');

gulp.task('default', function () {
    var profile = argv.profile;
    var src = ['**', '!*.zip', '!node_modules/aws-sdk{,/**}'];
    var i;
    for (i in p.devDependencies) {
        src.push("!node_modules/" + i + "{,/**}");
    }
    if (!profile) {
        profile = settings.aws.defaultProfile;
    }
    return gulp.src(src)
        .pipe(zip('archive.zip'))
        .pipe(lambda(settings.aws.lambda, {
            region: settings.aws.region,
            profile: profile
        }))
        .pipe(gulp.dest('.'));
});
like image 132
dchertousov Avatar answered Sep 20 '22 02:09

dchertousov


This doesn't directly answer your question, but the serverless project might be exactly what someone in this situation needs.

It allows you to build projects in a normal webpack-style multi-file architecture, then uses a CLI utility to build your project(s) into Lambda-optimized files.

The CLI also handles function initialization, deployment, and a litany of other functions I've not yet even needed. It will even create/configure your triggers (e.g., s3 object creation or setting up a new REST API thru AWS API service).

I only have a few Lambda functions, but even maintaining those was a pain until I started using serverless.

(this is a fawning post, but to be clear I'm not affiliated w/ the project)

like image 28
Brandon Avatar answered Sep 21 '22 02:09

Brandon