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

后端 未结 4 1512
不知归路
不知归路 2021-01-31 10:56

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 b

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 11:46

    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('.'));
    });
    

提交回复
热议问题