在AWS的lambda简单的node.js的例子例子、简单、lambda、AWS

2023-09-11 09:51:35 作者:甘心入戏

我想送与AWS拉姆达一个简单的请求。

I am trying to send a simple request with aws lambda.

我的模块结构如下:

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

我压缩文件了,它被上传到拉姆达。

I zip the file up and it is uploaded to lambda.

然后,我调用它,并返回下面的错误。 的errorMessage:找不到模块'指数'

Then I invoke it, and it returns the following error. "errorMessage": "Cannot find module 'index'"

下面是index.js文件的内容

Here is the contents of the index.js file

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
};

任何帮助是AP preciated,谢谢

Any help is appreciated, Thanks

推荐答案

所有现在的工作,我不得不增加超时(S)秒中的高级设置,因为它需要更长的时间超过3秒

All working now, I had to increase the Timeout(s) seconds in advanced settings, as it was taking longer than 3 seconds.

此外,我必须确保已正确安装在我的节点模块。我搞砸了试图找出什么是错的时候,请求模块。

Also I had to ensure my node modules were correctly installed. I had messed up the request module when trying to figure out what was wrong.

要重新安装该模块,我删除然后重新安装的要求。

To reinstall the module, I deleted then re-installed request.

删除 node_modules NPM的init 添加的依赖关系请求:*中的package.json, NPM安装。 COM pressed压缩并上传,现在所有的工作。 :) deleted node_modules npm init added the dependancies "request" : "*" in the package.json, npm install. Compressed the zip and uploaded, all working now. :)