我有一个使用无服务器,快速和libxmljs(将JavaScript绑定到libxml)的超基本AWS Lambda函数:
serverless.xml:
service: xmltest
provider:
name: aws
runtime: nodejs10.x
stage: dev
region: us-east-1
functions:
app:
handler: index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
plugins:
- serverless-offline
index.js:
const serverless = require('serverless-http');
const express = require('express');
const libxml = require("libxmljs");
const app = express();
app.use(express.json());
app.post('/lookup', async function (req, res) {
res.send({result: "hello world"});
});
module.exports.handler = serverless(app);
当我在本地跑步时:
$ curl -X POST http://localhost:3000/lookup -d {"example":123}
{"result":"hello world"}
当我在AWS上运行时:
$ curl -X POST https://REDACTED.execute-api.us-east-1.amazonaws.com/dev/lookup -d {"example":123}
{"message": "Internal server error"}
CloudWatch日志说:
2019-06-05T12:46:43.280Z undefined ERROR Uncaught Exception
{
"errorType": "Error",
"errorMessage": "/var/task/node_modules/libxmljs/build/Release/xmljs.node: invalid ELF header",
"stack": [
"Error: /var/task/node_modules/libxmljs/build/Release/xmljs.node: invalid ELF header",
" at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)",
" at Module.load (internal/modules/cjs/loader.js:600:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:539:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:531:3)",
" at Module.require (internal/modules/cjs/loader.js:637:17)",
" at require (internal/modules/cjs/helpers.js:22:18)",
" at bindings (/var/task/node_modules/bindings/bindings.js:84:48)",
" at Object.<anonymous> (/var/task/node_modules/libxmljs/lib/bindings.js:1:99)",
" at Module._compile (internal/modules/cjs/loader.js:701:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)"
]
}
如何在AWS上导入libxmljs?
我看到一些相关的问题,例如
因此,我猜测libxmljs中的某些内容是为与Amazon Linux不同的体系结构(我的本地计算机为macOS)构建的。但是我不确定该如何解决?
答案 0 :(得分:0)
我通过在Linux而非Mac上运行npm install
来解决了这个问题。
就我而言,我设置了AWS CodePipeline,它在EC2实例上运行所有构建脚本。也可以使用其他托管CI管道,Docker容器等解决此问题。
答案 1 :(得分:0)
我在Pusher上也遇到了同样的问题。阅读@ aaron-brager的答案后,我意识到了这个问题,然后尝试一下。它对我有用。
#!/bin/bash
export PKG_DIR="python/lib/python3.6/site-packages/"
rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR}
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.6 \
pip3 install -r requirements.txt -t ${PKG_DIR}