简体   繁体   English

AWS Lambda function 返回 Runtime.HandlerNotFound 错误

[英]AWS Lambda function returning Runtime.HandlerNotFound error

I am new to AWS Lambdas - I am trying to write a lambda function to retrieve some data from firebase. I have called this function exports.handler and uploaded it to the Lambda with the node modules as a Zip file.我是 AWS Lambdas 的新手 - 我正在尝试编写一个 lambda function 以从 firebase 检索一些数据。我将这个 function exports.handler 称为 Lambda 并将其作为 8820145828117 文件上传到 Lambda However, when I try to run it, it returns the following error:但是,当我尝试运行它时,它返回以下错误:

{
  "errorType": "Runtime.HandlerNotFound",
  "errorMessage": "index.handler is undefined or not exported",
  "trace": [
    "Runtime.HandlerNotFound: index.handler is undefined or not exported",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1156:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)",
    "    at Module.load (internal/modules/cjs/loader.js:1000:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:899

Here is my function:这是我的 function:

module.exports.handler = async (event) =>{
    ref.on("value", function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            responses.push({
                date: childSnapshot.val().Date,
                name:childSnapshot.val().name,
                response: childSnapshot.val().response
            })
        });
        printObjects(responses);
        console.log(json(responsesByPerson));
    })
})

I have looked at the other answers to questions similar to mine and implemented some of those solutions but none have worked.我查看了与我的问题类似的其他答案,并实施了其中一些解决方案,但都没有奏效。

When we zip a directory the shell directory is also added to the zip while the Lambda expects it to be without the shell directory.当我们 zip 一个目录时,shell 目录也被添加到 zip 而 Lambda 期望它没有 shell 目录。 Use the below command to create the zip correctly.使用以下命令正确创建 zip。

$ cd my_lambda_fun
$ zip -r lambda_one.zip .

You have to make sure that you don't zip your project but the contents of your project.您必须确保不是 zip 您的项目,而是项目的内容。

For eg, if your index.js is directly inside my-project , you should $> cd my-project and then create a zip with the contents.例如,如果你的index.js直接在my-project中,你应该$> cd my-project然后用内容创建一个 zip 。 This zip can then be uploaded to AWS Lambda.然后可以将这个 zip 上传到 AWS Lambda。

I hope this helps!我希望这有帮助!

Ensure index.js is in the root of the Lambda function directory确保 index.js 位于 Lambda function 目录的根目录中

zip -r lambda_one.zip. zip -r lambda_one.zip。 worked for me为我工作

Some of the answers here are indeed correct, but if you're like me and trying to do this from Windows then it should be noted that $ zip -r lambda_one.zip.这里的一些答案确实是正确的,但如果你像我一样并试图从 Windows 开始这样做,那么应该注意$ zip -r lambda_one.zip. will not work from PowerShell as it will in Linux and macOS.从 PowerShell 开始不起作用,就像在 Linux 和 macOS 中一样。

The workaround was simply to zip the folders + files manually in the file explorer:解决方法是在文件资源管理器中手动将文件夹 + 文件设置为 zip:

  1. Select folders + files to be zipped Select 个文件夹+待压缩文件
  2. Right click > Send to > Compressed (zipped) folder右键单击 > 发送到 > 压缩 (zipped) 文件夹
  3. Rename it to whatever you need, then proceed with uploading it to Lambda when you create your function.将其重命名为您需要的任何名称,然后在创建 function 时继续将其上传到 Lambda。

This was one error I also got while Trying to execute the NodeJs Code.这是我在尝试执行 NodeJs 代码时也遇到的一个错误。 The following are the points we need to take care:以下是我们需要注意的几点:

  1. Lambda Handler must be "filename.handler" Lambda 处理程序必须是“filename.handler”
  2. filename.js must Exist in the Root Directory of the Zip Folder that would be uploaded to Lambda Function. filename.js 必须存在于将上传到 Lambda Function 的 Zip 文件夹的根目录中。
  3. While creating the Zip Folder - Open the Folder and use "Select All" before using "Send To" Compressed.(The benefit is the filename.js would be reflected in the Root itself, rather than Creating Zip folder for the Application Project Folder).在创建 Zip 文件夹时 - 打开文件夹并在使用“发送到”压缩之前使用“全选”。(好处是 filename.js 将反映在根目录本身中,而不是为应用程序项目文件夹创建 Zip 文件夹) . This has worked, for the Lambda function Execution for NodeJS Project.这对 Lambda function NodeJS 项目的执行有效。

before the this line def handler(event, context):在此行 def 处理程序(事件,上下文)之前:

if you have any imports, then remove those.如果您有任何进口商品,请将其删除。 It should work.它应该工作。

Be careful for not being using a wrong Running engine like NodeJS but writing code in Python or something of that nature.注意不要使用像 NodeJS 这样错误的运行引擎,而是在 Python 或类似的东西中编写代码。 Go to the Lambda function info link and select the correct Running Engine for your development. Go 到 Lambda function 信息链接和 select 为您的开发提供正确的运行引擎。

Make sure to name your handler file as index.js other you will get this error确保将您的处理程序文件命名为index.js否则您将收到此错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM