Search code examples
node.jsmacosamazon-web-servicesserverless-frameworksharp

How can I deploy a Serverless Framework application the macOS that uses the sharp library to AWS?


I'm developing a Serverless Framework application that is using the Node runtime and is deployed to AWS. One of my AWS Lambda functions uses the sharp library.

When I run the AWS Lambda function, the following error occurs:

'darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.

I believe this error is occurring because when I run the sls deploy command on my local computer, the application is packaged on macOS and then moved to AWS. I think the application needs to be packaged on an operating system using linux-x64.

How can I deploy my Serverless Framework from my computer and still be able to use the sharp library?


Solution

  • You can install the Linux compatible package using the following:

    rm -rf node_modules/sharp
    npm install --arch=x64 --platform=linux --target=10.15.0 sharp
    

    Note that this also specifies a target NodeJS version, ensure its the same version of node you're using in your Lambda. This is straight out of the docs (see here.)

    However that didn't solve my problems. My serverless configuration (using serverless-bundle plugin) meant that my modules were being installed again in a separate folder, wiping out the platform-specific modules I just manually installed.

    Two choices here:

    1. use serverless-plugin-scripts to hook into the deploy events to run the above patch; or
    2. run serverless in docker using a Linux container with a matching node version.

    For my specific edge case I had to go with Docker. The build scripts will effect every function you're deploying -- adding ~30mb of Sharp code -- and Lambda@Edge has limitations on source code size.