简体   繁体   English

AWS和Web3部署中的Lambda函数错误

[英]AWS and web3 deployment in lambda function error

When i am doing upload web3 modules on AWS lambda function then getting this error.Please tell me what is reason 当我在AWS lambda函数上上传web3模块时出现此错误。请告诉我原因是什么

{
  "errorMessage": "/var/task/node_modules/scrypt/build/Release/scrypt.node: invalid ELF header",
  "errorType": "Error",
  "stackTrace": [
    "Object.Module._extensions..node (module.js:597:18)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "Object.<anonymous> (/var/task/node_modules/scrypt/index.js:3:20)",
    "Module._compile (module.js:570:32)",
    "Object.Module._extensions..js (module.js:579:10)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "Object.<anonymous> (/var/task/node_modules/scrypt.js/node.js:1:76)",
    "Module._compile (module.js:570:32)",
    "Object.Module._extensions..js (module.js:579:10)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)"
  ]
}

The problem is that your scrypt module is compiled for OSX and is not compatible with the OS running the lambdas. 问题是您的scrypt模块是为OSX编译的,并且与运行lambda的OS不兼容。 In serverless' git the issue is discussed. 无服务器的git中讨论了这个问题。

To bring the solutions here, a couple are given, first one is by Kennu , he suggest to add a custom "install" script in the package.json: 为了将解决方案带到这里,给出了几个示例 ,第一个是Kennu提出的 ,他建议在package.json中添加一个自定义的“安装”脚本:

"install": "[ -e node_modules/sharp/build/Release/sharp.node ] || docker run --rm -v $PWD:/data -w /data node:4 npm install sharp"

The solution I used was given by jokeyrhyme , he suggest to use the following script to be able to run npm install within a docker instance. 我使用的解决方案由jokeyrhyme提供 ,他建议使用以下脚本来能够在docker实例中运行npm install。 I copy the code here: 我在这里复制代码:

'use strict'

// ideal for use with AWS Lambda and native Node.js modules

// requires Docker: https://docs.docker.com/engine/installation/

/*
Usage:
  node docker-npm.js install
  node docker-npm.js rebuild
*/

const childProcess = require('child_process')

const nodejsImage = 'node:4.3'
const innerWorkingDir = '/src'
const dockerArgs = [
  'run', '-i',
  '-v', `${process.cwd()}:${innerWorkingDir}`,
  '-w', innerWorkingDir,
  nodejsImage, 'npm'
]
const npmArgs = process.argv.slice(2)

const cp = childProcess.execFile(
  'docker',
  dockerArgs.concat(npmArgs),
  {},
  (err, stdout, stderr) => {}
)

cp.stderr.on('data', (data) => console.error(data))
cp.stdout.on('data', (data) => console.log(data))

cp.on('close', (code) => process.exit(code))

If anyone is still having issues with running web3 > 1.0.0 and you're using SAM local to test it, consider running sam build --use-container prior to executing the lambda with sam invoke local . 如果仍然有人在运行web3> 1.0.0时遇到问题,并且您正在使用SAM local进行测试,请考虑先运行sam build --use-container然后再使用sam invoke local执行lambda。

This will build and install any dependencies inside of a amazon linux container instead of building it locally. 这将在amazon linux容器内构建并安装所有依赖项,而不是在本地构建它。

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

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