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?
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?
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
Update
Now that the aws-sdk package is deprecated, newer runtime versions (since nodejs18.x) only have @aws-sdk/* packages installed, instead of aws-sdk.
aws-sdk are preinstalled as well, such as lodash (though you shouldn't rely on that)Unfortunately the docs don't seem very specific about it, but you can grab their docker images yourself and bash in to inspect them:
docker run --rm -it --entrypoint /bin/bash amazon/aws-lambda-nodejs:14
The Dockerfile has ENTRYPOINT ["/lambda-entrypoint.sh"] and by inspecting that file I was able to determine that it runs Node via /var/runtime/bootstrap.
/var/runtime/bootstrap adds various directories to NODE_PATH from which modules can be loaded:
if [ -z "$NODE_PATH" ];
then
nodejs_mods="/opt/nodejs/node_modules"
nodejs14_mods="/opt/nodejs/node14/node_modules"
runtime_mods="/var/runtime/node_modules"
task="/var/runtime:/var/task"
export NODE_PATH="$nodejs14_mods:$nodejs_mods:$runtime_mods:$task"
fi
However /opt/nodejs doesn't exist in the image and /var/task is where Lambda puts your custom code.
So /var/runtime/node_modules is the only directory we need to inspect:
bash-4.2# cd /var/runtime
bash-4.2# npm ls
/var/runtime
└─┬ [email protected]
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
├── [email protected] extraneous
└── [email protected] extraneous
As you can see, only aws-sdk is preinstalled. But you could use this process to determine if they add more preinstalled deps in future versions of their Node.js runtime.
UPDATE
I repeated this process on amazon/aws-lambda-nodejs:20 and confirmed that now the AWS Node SDK v3 packages are installed instead:
bash-5.2# npm list
npm notice
npm notice New minor version of npm available! 10.5.2 -> 10.8.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.1
npm notice Run npm install -g [email protected] to update!
npm notice
/var/runtime
├── @aws-sdk@ extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
├── @aws-sdk/[email protected] extraneous
I couldn't find an official list so I wrote a script to create a list.
I have a number of other libraries installed in my Lambda, those are (excluding built-in nodejs modules which are also available of course):
'aws-sdk', <-- preinstalled by AWS
'awslambda',
'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.
aws-sdk. github.com/aws/aws-sdk-js/blob/… Relying on or require-ing those is a dangerous because those are internal to aws-sdk library. Also, the imagemagick in your list looks suspicious, it's a fairly large library. It might be coming from your Lambda Layers - docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html which is out of the scope of this question.imagemagick is preinstalled.imagemagick was not preinstalled for me. I created a "from scratch" Lambda function with the Node.js 12.x. runtime. Adding im = require('imagemagick'); caused the function to fail with Error: Cannot find module 'imagemagick'.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.
What this means is that for tenants who:
will get an error related to missing node_modules post transition.