javascript - AWS Lambda 的打包代码

标签 javascript node.js amazon-web-services aws-lambda

我正在尝试打包 AWS Lambda 的代码。 Lambda 有各种限制,例如使用 Node 6.10,并且没有像 AWS EB 那样的构建步骤。我还使用 NPM 模块,因此这些模块需要与 AWS Lambda 处理程序捆绑在一起。

这是我想做的:

  • 定义和使用 NPM 模块(仅限纯 JS 模块)
  • 将所有代码(包括 NPM 模块)转换为 Node 6.10 支持的 JS 版本
  • 将所有 NPM 模块静态链接到一个大 JS 文件
  • 将该单个文件上传到 AWS Lambda

例如,假设我有一个 NPM 模块 foo (node_modules/foo/index.js):

export default { x: 1 };

我有自己的代码('index.js'):

import foo from 'foo';

export const handler = (event, context, callback) => {
  console.log(foo); // Will appear in CloudWatch logs
  callback(null, 'OK');
};

输出将是这样的('dist/bundle.js'):

var foo = { x: 1 };

exports.handler = function(event, context, callback) {
  console.log(foo);
  callback(null, 'OK');
};

我应该能够在 AWS Lambda 上上传并运行 bundle.js ,而无需进一步修改。

如何使用现有的 JS 工具实现此目的?

最佳答案

您可以使用serverlessserverless-webpack

然后使用无服务器部署部署 bundle

关于javascript - AWS Lambda 的打包代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47905321/

相关文章:

javascript - Lodash orderBy 对象数组的对象

visual-studio-2008 - 如何解决 AWS Elastic Beanstalk 部署错误

javascript - 如何在casperjs代码中设置代理

javascript - JSON.parse(JSON.stringify(e)) 将对象转换为字符串

javascript - Node js 添加所需的模型

javascript - 使用 express 在客户端显示图像的字符串表示形式

javascript - Promise then() 和 catch() UnhandledPromiseRejectionWarnin

amazon-web-services - AWS SNS 动态订阅

amazon-web-services - AWS CodePipeline "An AppSpec file is required, but could not be found in the revision"

javascript - AngularJS:验证 ng-disabled/ng-readonly 字段