4

aws lambda with typescript is occured error when bundle by webpack.

Before bundle webpack, I did "npm i --arch=x64 --platform=linux --target=12.14.1 sharp" and labmda is working properly.

But, lambda upload zip size is more and more larger.

so, I want to resize lambda upload zip size using serverless-webpack.

Image lambda is working well except using sharp module.

I don't know how to do.

I did:

  1. Delete node_modules and package-lock.json and install dependencies ( also installed sharp )
  2. Delete node_modules/sharp and install sharp ( lambda environment - linux, x64, node version )
  3. Set serverless-webpack configuration in serverless : packagerOptions ( scrips ) - rebuild sharp lambda enviroment

But, lambda is not working properly.

and I looked many informations.

[lambda linux env]
Error running Sharp inside AWS Lambda function: darwin-x64' binaries cannot be used on the 'linux-x64' platform

[Serverless-webpack]
https://github.com/serverless-heaven/serverless-webpack/issues/396

Thank you!


[Edit]

My local env : Mac

production env : linux

Maybe, I think that npm command with "--platform" is not working in mac.

Additionally, I solved this problem using aws codebuild.

I posted answer.

But, It is not working in my local [ Mac ]

hyundeock
  • 445
  • 1
  • 6
  • 15

4 Answers4

8

I got it working by telling webpack to reinstall the package after it runs npm install:

webpack:
  includeModules:
    forceExclude:
      - aws-sdk
  packagerOptions:
    scripts:
      - rm -rf node_modules/sharp
      - npm install --arch=x64 --platform=linux sharp
Dave Cowart
  • 781
  • 6
  • 17
  • when doing that it adds sharp to the all the functions. Did you find anyway that make it bundle to only the function using sharp? – Aecio Levy Sep 09 '20 at 13:46
  • @AecioLevy Fortunately for me, and unfortunately for you, in my particular case I only have one function. But this GitHub issue implies there's not an easy way without rewriting a bunch of your serverless code: https://github.com/serverless-heaven/serverless-webpack/issues/525 – Dave Cowart Sep 11 '20 at 04:15
  • thank you so much. I end up doing this ``` - if [ -f "api/resize.js" ]; then rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux sharp; fi``` but serverless component seems to a better idea. – Aecio Levy Sep 12 '20 at 16:30
5

I solved this problem using aws codebuild.

codebuild has linux and node.js runtime.

So, I ran below command in aws codebuild ( https://sharp.pixelplumbing.com/install )

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

sharp module works properly.

hyundeock
  • 445
  • 1
  • 6
  • 15
  • Many answers tell "using serverless.yml". But, it is not working properly. Refer to sharp github issue (https://github.com/lovell/sharp/issues/2230) – hyundeock Aug 03 '20 at 05:38
2

Dave Cowart's answer helped me a lot but having multiple lambda functions in the repo, I didn't want to install sharp into all of my functions.

Here's my solution:

serverless.yml

  webpack:
    webpackConfig: 'webpack.config.js'
    includeModules:
      forceExclude:
        - aws-sdk
    packagerOptions:
      scripts:
        - rm -rf node_modules/sharp
        - npm install --production --arch=x64 --platform=linux

You don't actually need to specify sharp in the install script. Making it a generic npm install means it will only reinstall sharp if it's in the package.json (which it won't be if it's not used due to webpack tree shaking).

Oli
  • 130
  • 1
  • 5
1

I got it working by using sam cli to build using this command

sam build -u

This command builds the code inside a container that has the similar environments as lambda

kabangi julius
  • 2,709
  • 2
  • 16
  • 25