Which NPM modules are preinstalled in AWS Lambda execution environment?

后端 未结 3 1674
攒了一身酷
攒了一身酷 2021-01-02 13:35

Recently I found that aws-sdk NPM module is preinstalled in AWS Lambda nodejs8.10. And I can\'t find any information in the internet about it.

Which oth

相关标签:
3条回答
  • 2021-01-02 13:42

    Only the aws-sdk package is preinstalled .

    All the rest is loaded from the "node_modules" directory..

    You can find information about it here:

    https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html

    0 讨论(0)
  • 2021-01-02 13:50

    I've used the "https" and the "url" package, so those at least is pre-installed. There are quite a few standard node.js modules which need a native layer.

    Clearly the AWS modules are in there, for communicating with AWS services. I've used SQS, for example.

    Haven't tried "fs" yet, but since it requires a native layer, and is something you might want to do (e.g. persisting stuff to /tmp) I'm assuming it's there.

    Somewhere there ought to be a list. But I can't find one. Guess you just have to try, and if the requires fails, then you need to put a module in node_modules, then see if it demands dependencies.

    0 讨论(0)
  • 2021-01-02 14:01

    I couldn't find an official list so I wrote a script to create a list. Currently these are (excluding built-in nodejs modules which are also available of course):

    'awslambda',
    'aws-sdk',
    'base64-js',
    'dynamodb-doc',
    'ieee754',
    'imagemagick',
    'isarray',
    'jmespath',
    'lodash',
    'sax',
    'uuid',
    'xml2js',
    'xmlbuilder'
    

    Code to generate this list:

    function flatten(arrayOfArrays) {
        return Array.prototype.concat.apply([], arrayOfArrays)
    }
    
    function onlyUnique(value, index, self) {
        return self.indexOf(value) === index;
    }
    
    function extPackageNames(node) {
        if (!node.children) return [];
        const arrayOfArrays = node.children.map(c => [c.name].concat(extPackageNames(c)))
        const result = flatten(arrayOfArrays)
        return result
    }
    
    exports.handler = async (event) => {
        const rpt = require("read-package-tree")
        const module = require("module")
    
        const pathArg = process.env.NODE_PATH
        const allPaths = pathArg.split(":")
    
        // '/var/task' is this package on lambda
        const externalPaths = allPaths.filter(p => p !== "/var/task")
    
        // read all package data
        const ps = externalPaths.map((path) => rpt(path).catch(err => err))
        const rpts = await Promise.all(ps).catch(err => err)
    
        // extract the information we need
        const packagesPerPath = rpts.map(extPackageNames)
        const countPerPath = packagesPerPath.map(arr => arr.length)
        const packages = flatten(packagesPerPath)
    
        // remove duplicates
        const uniquePackages = packages.filter(onlyUnique)
    
        // remove node.js built-in modules
        const uniqueCustomPackages = uniquePackages.filter(p => !module.builtinModules.includes(p))
    
        const result = {
            node_path: pathArg,
            paths: externalPaths.map((e, i) => [e, countPerPath[i]]),
            uniqueCustomPackages
        }
    
        console.log(result)
    
        const response = {
            statusCode: 200,
            body: JSON.stringify(result)
        };
        return response;
    };
    

    To run this on lambda you will need to zip it together with a node_modules folder containing read-package-tree.

    0 讨论(0)
提交回复
热议问题