我將一個小的Lambda函數放在一起,使用SpookyJS,CasperJS和PhantomJS工具鏈來抓取網站以進行無頭瀏覽。任務很簡單,幾個月前它在Lambda工作。我最近不得不改變一些東西,並希望再次在該項目上工作,但開始新鮮並且無法讓Lambda在沒有任何錯誤的情況下運行。我的問題是如何在Lambda中運行phantomjs?如何在AWS Lambda上部署phantomjs節點應用程序?
我運行的代碼示例是:
spooky.start('http://en.wikipedia.org/wiki/Spooky_the_Tuff_Little_Ghost');
spooky.then(function() {
this.emit('hello', 'Hello, from ' + this.evaluate(function() {
return document.title;
}));
});
spooky.run();
我在拉姆達得到的錯誤是:
{ [Error: Child terminated with non-zero exit code 1] details: { code: 1, signal: null } }
我按照各種程序,以確保一切都能夠在運行LAMBDA。下面是一個長期的事情,我已經嘗試診斷列表:
- 運行在本地使用
node index.js
,並確認這是工作 - 上傳的package.json和js文件到一個Amazon Linux的EC2實例進行編譯推薦對於NPM安裝電話和在EC2實例描述here
- 運行
npm install
,並再次運行node index.js
,以確保正確的輸出 - 拉鍊的一切行動,並使用CLI
我的package.json是:
{
"name": "lambda-spooky-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"casperjs": "^1.1.3",
"phantomjs-prebuilt": "^2.1.10",
"spooky": "^0.2.5"
}
}
我也曾嘗試以下(也最多在本地工作,並在AWS EC2實例,但對LAMBDA相同的錯誤:
- 嘗試幻影的非-prebuilt版本
- 確保casperjs和phantomjs是從
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/.bin'; console.log('PATH: ' + process.env.PATH);
檢查產卵呼叫b的路徑訪問Ÿ包裝child_process的
.spawn()
電話,並得到了以下內容:{ '0': 'casperjs', '1': [ '/var/task/node_modules/spooky/lib/bootstrap.js', '--transport=http', '--command=casperjs', '--port=8081', '--spooky_lib=/var/task/node_modules/spooky/lib/../', '--spawnOptions=[object Object]' ], '2': {} }
調用
.exec('casperjs')
和.exec('phantomjs --version')
直接,確認它工作在本地和EC2,但得到以下錯誤LAMBDA。命令:`require('child_process').exec('casperjs', (error, stdout, stderr) => { if (error) { console.error('error: ' + error); } console.log('out: ' + stdout); console.log('err: ' + stderr); });
都結果如下:
err: Error: Command failed: /bin/sh -c casperjs
module.js:327
throw err;
^
Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
2016-08-07T15:36:37.349Z b9a1b509-5cb4-11e6-ae82-256a0a2817b9 sout:
2016-08-07T15:36:37.349Z b9a1b509-5cb4-11e6-ae82-256a0a2817b9 serr: module.js:327
throw err;
^
Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
萬一其他人發現這種情況,並遭受類似的問題。即使在上述情況之後,我仍然患有「以非零退出代碼1終止的兒童」原來,我不得不進入node_modules下的casperjs目錄並運行npm_update,因爲它缺少從源代碼無法正確獲取的依賴關係父母 – Nick