AWS Lambda 错误: “无法找到模块’/var/task/index’”

Alexa 任务问题

我目前正在通过 AWS Lambda 编写一个 Node.js Alexa Task,我一直在尝试编写一个函数,该函数接收来自 OpenWeather API 的信息,并将其解析为一个名为 weather的变量。有关守则如下:

var request = require('request');
var weather = "";
function isBadWeather(location) {
var endpoint = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&APPID=205283d9c9211b776d3580d5de5d6338";
var body = "";
request(endpoint, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
weather = body.weather[0].id;
}
});
}


function testWeather()
{
setTimeout(function() {
if (weather >= 200 && weather < 800)
weather = true;
else
weather = false;
console.log(weather);
generateResponse(buildSpeechletResponse(weather, true), {});
}, 500);
}

我在 Cloud9和其他 IDE 中无数次运行这个代码片段,它似乎工作得完美无缺。但是,当我将它压缩到一个包中并将其上传到 AWS Lambda 时,会得到以下错误:

{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)"
]
}

我安装了 module-js、 request 和许多其他 Node 模块,这些模块应该可以运行这段代码,但似乎没有什么能够解决这个问题。这是我的目录,以防万一:

- planyr.zip
- index.js
- node_modules
- package.json

有人知道问题出在哪里吗?

69981 次浏览

This is probably a permissions issue with files inside your deployment zip. Try chmod 777 your files before packaging them in a zip file.

Fixed it! My issue was that I tried to zip the file using my Mac's built-in compression function in Finder.

If you're a Mac user, like me, you should run the following script in terminal when you are in the root directory of your project (folder containing your index.js, node_modules, etc. files).

zip -r ../yourfilename.zip *

For Windows:

Compress-Archive -LiteralPath node_modules, index.js -DestinationPath yourfilename.zip

In my case I had to replace

exports.handler = function eventHandler (event, context) {

with

exports.handler = function (event, context, callback) {

Update to the accepted answer: When this error occurs, it means your zip file is not in the valid form which AWS requires.

If you double click on zip you will find your folder inside that your code file,but lambda wants that when you double click on zip it shoud show direct code files.

To achieve this:

open terminal
cd your-lambda-folder
zip -r index.zip *

Then, upload index.zip to AWS Lambda.

Check that file name and handler name are same:

In this case we expect that all our code will be in <code>bundle.ls</code> file

That means that zip file has bundle.js file that exports handler function:

exports.handler = (event, context, callback) => {//...}

In my case the archive contained a folder "src" with index.js file, so I had to put to the handler: "src/index.handler"

enter image description here

In my case it was because I had the handler file in inner src directory.

I had to change the 'Handler' property within Lambda from:

index.handler

to

src/index.handler

I got this error when I was using lambci/lambda:nodejs8.10 in windows.

I'd tried all of the solution listed above but none of which could help me deal with my issue(even though the error stack look the same as the question).

Here is my simple solution:

  1. using --entrypoint flag to run a container to find out if the file is mounted into the container. It turns out I may got the share drive issue with my Docker Desktop.
  2. I switched my docker daemon that day before, but everything works fine except this problem.
  3. Anyway, remount my drive to Docker Desktop, you can both use the docker command or just open the Docker Desktop setting to apply.

In my case this was caused by Node running out of memory. I fixed that by adding --memory-size 1500 to my aws lambda create-function ... command.