Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Invalid ELF header" using libxmljs on AWS Lambda

Tags:

I have a super basic AWS Lambda function using serverless, express, and libxmljs (which binds JavaScript to libxml):

Code

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);

Works Locally But Not on AWS

When I run locally:

$ curl -X POST http://localhost:3000/lookup -d {"example":123}

{"result":"hello world"}

When I run on AWS:

$ curl -X POST https://REDACTED.execute-api.us-east-1.amazonaws.com/dev/lookup -d {"example":123}

{"message": "Internal server error"}

The CloudWatch logs say:

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)"
    ]
}

Question

How can I import libxmljs on AWS?

I see some related questions like

  • aws - “Unable to import module 'process': /var/task/numpy/core/multiarray.so: invalid ELF header”
  • “invalid ELF header” when using the nodejs “ref” module on AWS Lambda

So I’m guessing something in libxmljs is built for a different architecture (my local machine is macOS) than Amazon Linux. But I'm not sure how I can fix this?

like image 843
Aaron Brager Avatar asked Jun 05 '19 12:06

Aaron Brager


1 Answers

I solved the problem by running npm install on a Linux box instead of my Mac.

In my case I set up AWS CodePipeline which runs all the build scripts on an EC2 instance. Could also have solved this using other hosted CI pipeline, a Docker container, etc.

like image 59
Aaron Brager Avatar answered Oct 05 '22 04:10

Aaron Brager