Which NPM modules are preinstalled in AWS Lambda execution environment?

时光总嘲笑我的痴心妄想 提交于 2020-06-22 10:11:23

问题


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 other node.js modules are pre-installed in AWS Lambda?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/53566478/which-npm-modules-are-preinstalled-in-aws-lambda-execution-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!