How can I install GraphicsMagick or ImageMagick on AWS Lambda?

前端 未结 7 1463
灰色年华
灰色年华 2020-11-29 20:18

I am using the gm package for Node.js along with the default ImageMagick installation that is available on AWS Lambda.

const gm = require(\'gm\'

相关标签:
7条回答
  • 2020-11-29 21:13

    I was struggling on this for a couple of days, ended up going through the process myself and it does indeed work.

    ImageMagick is no longer bundled with the Node.js 10.x runtime. There are 3 options to get ImageMagick working with your Node.js 10.x function:

    1) Package the dependency and include it in your uploaded ZIP file (like this one)

    https://image-magick-example.s3-us-west-2.amazonaws.com/image-magick-example.zip

    https://github.com/hmagdy/imagemagick-aws-lambda-Node.js10.x/tree/master/option1_image-magick-example-zip

    But with option: The deployment package of your Lambda function "image-magick-example-zip-demo" is too large to enable inline code editing. However, you can still invoke your function.

    or

    2) Create or use a Lambda Layer that includes ImageMagick, to do that:

    clone git@github.com:hmagdy/imagemagick-aws-lambda-Node.js10.x.git
    cd imagemagick-aws-lambda-2
    start Docker services
    make all
    

    That would create a layer.zip inside build folder. But to save you some time here’s a zip file you can use to create a Lambda Layer.

    https://image-magick-layer.s3-us-west-2.amazonaws.com/layer.zip

    When you create the layer make sure you add Node.js 10.x as a supported runtime. You can then set your function to use the latest Node.js 10.x and add the layer you created. The image conversion should then work again!

    Then you can create your aws lambda function like this

    https://github.com/hmagdy/imagemagick-aws-lambda-Node.js10.x/tree/master/option2_image-magick-example-c_lib_layer/index.js

    3) NodeJS Runtime Environment (npm) with AWS Lambda Layers, to do that:

    Also, if you want to use

    const imageThumbnail = require('image-thumbnail');
    

    and got

    Runtime.ImportModuleError: Error: Cannot find module 'image-thumbnail'
    

    you should follow option 3:

    https://github.com/hmagdy/imagemagick-aws-lambda-Node.js10.x/tree/master/option3_image-magick-example-npm_layer

    Inspired by:

    https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e

    0 讨论(0)
提交回复
热议问题