aws lambda中的简单node.js示例

我正在尝试用aws lambda发送一个简单的请求。

我的模块结构如下:

mylambda |-- index.js |-- node_modules | |-- request 

我压缩文件,并将其上传到lambda。

然后我调用它,并返回以下错误。 "errorMessage": "Cannot find module 'index'"

这里是index.js文件的内容

 var request = require('request'); exports.handler = function(event, context) { var headers = { 'User-Agent': 'Super Agent/0.0.1', 'Content-Type': 'application/x-www-form-urlencoded' } // Configure the request var options = { url: 'https://myendpoint', method: 'POST', headers: headers, form: {'payload': {"text":""} } } // Start the request request(options, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) } }) console.log('value1 =', event.key1); context.succeed(event.key1); // Echo back the first key value }; 

任何帮助表示赞赏,谢谢

现在所有的工作,我不得不提高超时(秒)在高级设置,因为它需要超过3秒。

另外,我必须确保我的节点模块已正确安装。 试图弄清楚什么是错误的时候,我搞砸了请求模块。

重新安装模块,我删除然后重新安装请求

  • 删除了node_modules
  • npm init
  • 在package.json中添加依赖"request" : "*"
  • npm install 。 压缩邮件并上传,所有工作现在。 🙂

您只需压缩并上传子文件夹,而不是根文件夹。 您必须根据您的示例压缩以下文件夹,然后上传:

 |-- index.js |-- node_modules |-- request