AWS Javascript SDK 仅安装特定模块而不是整个 SDK

AWS Javascript SDK install only specific modules instead of entire SDK

我在我的项目中使用 Lambda 函数(Nodejs 运行时)并使用来自 aws-sdk 的一些模块。

到目前为止,我已经安装了整个 aws-sdk 并且我需要像这样分别安装每个包:

const ApiGatewayManagementApi = require('aws-sdk/clients/apigatewaymanagementapi');

这可行,但问题是我需要安装整个 aws-sdk,它很大,因此使我的功能包很大。

有没有办法只安装我实际使用的模块?

我试过这个:

$ npm install aws-sdk/clients/apigatewaymanagementapi --save

这给了我以下错误:

npm ERR! code ENOLOCAL
npm ERR! Could not install from "aws-sdk/clients/apigatewaymanagementapi" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/gustavocsdc/.npm/_logs/2019-05-16T02_32_28_129Z-debug.log

遗憾的是,在 AWS JavaScript SDK 的当前版本 (2.x) 中,这是不可能的。有一个新的 AWS SDK for JavaScript V3 正在解决这个问题,但它仍在 "Developer Preview"(2019 年 5 月 16 日)。

同时,我们鼓励您为 JavaScript Lambda 函数安装完整的 aws-sdkLambda Application Best Practices 指出:

Control the dependencies in your function's deployment package. The AWS Lambda execution environment contains a number of libraries such as the AWS SDK for the Node.js and Python runtimes (a full list can be found here: AWS Lambda Runtimes). To enable the latest set of features and security updates, Lambda will periodically update these libraries. These updates may introduce subtle changes to the behavior of your Lambda function. To have full control of the dependencies your function uses, we recommend packaging all your dependencies with your deployment package.

可以看出,如果您愿意做出上述权衡,则将 sdk 包含在 Lambda 包中(从而减小其大小)并不是严格要求。您可能考虑将 aws-sdk 作为您的包的一部分提供的另一个原因是,您想要使用的更新版本的 sdk 中可能有一个新功能,而该功能不存在于 sdk 提供的运行时。

为了完整性,nodejs10.x 运行时提供 aws-sdk 的版本 2.437.0nodejs8.10 运行时提供 2.290.0 aws-sdk 版本AWS Lambda Runtimes.