简体   繁体   中英

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

{
  "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. In serverless' git the issue is discussed.

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:

"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. 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 .

This will build and install any dependencies inside of a amazon linux container instead of building it locally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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