如何在AWS Lambda上安装GraphicsMagick或ImageMagick? [英] How can I install GraphicsMagick or ImageMagick on AWS Lambda?

查看:274
本文介绍了如何在AWS Lambda上安装GraphicsMagick或ImageMagick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js的 gm 包以及AWS Lambda上提供的默认ImageMagick安装。

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')。subClass({imageMagick:true});

出于某种原因,某些图像的调整大小功能失败。

For some reason, the resize functionality fails for certain images.

我使用Amazon Linux AMI(ami-hvm-2016.03.3.x86_64-gp2)创建了一个EC2实例。
我安装了可从 yum 获得的(旧)6.x版ImageMagick。当我在EC2实例上使用该安装运行我的脚本时,它会重现我在Lambda上运行代码时看到的失败,确认此版本的IM导致失败。

I created an EC2 instance with Amazon Linux AMI (ami-hvm-2016.03.3.x86_64-gp2). I installed the (old) 6.x version of ImageMagick that is available from yum. When I run my script with that install on the EC2 instance, it reproduces the failure I see when the code runs on Lambda, confirming it is something with this version of IM that is causing the failure.

如果我用 sudo yum install GraphicsMagick 安装GrpahicsMagick。这允许我的脚本无错误地执行调整。

If I install GrpahicsMagick with sudo yum install GraphicsMagick. This allows my script to perform the resizes without error.

const gm = require('gm')。subClass({imageMagick:false}) ;

但是,我不确定如何在我的无服务器部署中捆绑这个。如果我使用 sudo yum --installroot = / var / task install GraphicsMagick 将GraphicsMagick安装到与脚本相同的文件夹中,并使用此require语句运行我的脚本:

However, I'm not sure how to bundle this in my deploy with serverless. If I install GraphicsMagick to the same folder as the script with sudo yum --installroot=/var/task install GraphicsMagick, and run my script using this require statement instead:

const gm = require('gm')。subClass({imageMagick:false,appPath:'。/ usr / bin /'});

我在EC2实例上运行脚本时调整大小。但是,当我使用无服务器部署,并且脚本在Lambda中运行时,可执行文件似乎已损坏。 gm 在调用 gm(缓冲区)时出现以下错误.size(/*...*/)

The resizes work when I run my script on the EC2 instance. But when I deploy with serverless, and the script runs in Lambda, the executable appears to be broken. gm fails with the following error on a call to gm(buffer).size(/*...*/).

无法获得图像大小:错误:
{code:EPIPE,errno: EPIPE,系统调用:写}

如何构建可以无服务器部署的ImageMagick或GraphicsMagick版本?

How can I build a version of ImageMagick or GraphicsMagick that can be deployed with serverless?

推荐答案

我启动了最新的aws linux并运行了以下命令。

I spun up the latest aws linux and ran the commands below.

yum -y install gcc-c++ libpng-devel libjpeg-devel libtiff-devel wget
wget https://downloads.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.26/GraphicsMagick-1.3.26.tar.gz
tar zxvf GraphicsMagick-1.3.26.tar.gz
cd GraphicsMagick-1.3.26
./configure --prefix=/var/task/graphicsmagick --enable-shared=no --enable-static=yes
make
sudo make install
tar zcvf ~/graphicsmagick.tgz /var/task/graphicsmagick/

我把dir放到我的本地并丢了它在要压缩和部署的包中。我的布局类似于链接的aws repo代码,但是针对无服务器进行了修改。

I scp the dir down into my local and threw it in the package to be zipped and deployed. My layout is similar to the aws repo code linked, but modified for serverless.

Lambda代码:

// graphicsmagick dir is at the root of my project
const BIN_PATH = process.env['LAMBDA_TASK_ROOT'] + "/graphicsmagick/bin/";
const Gm = require('gm').subClass({ appPath: BIN_PATH });

// below is inside the handler
process.env['PATH'] = process.env['PATH'] + ':' + BIN_PATH;

serverless.yml

serverless.yml

package:
  artifact: /path/to/function.zip

我使用神器并建立自己的拉链。如果你遇到下面的问题我建议你这样做。
https://github.com/serverless/serverless/issues/3215

I use the artifact and build my own zip. If you run into the issue below I suggest you do that. https://github.com/serverless/serverless/issues/3215

# -y to keep the symlinks and thus reduce the size from 266M to 73M
cd lambda && zip -FS -q -r -y ../dist/function.zip *

抓住想法:

https://gist.github。 com / bensie / 56f51bc33d4a55e2fc9a

https://github.com/awslabs/serverless-image-resizing

这篇关于如何在AWS Lambda上安装GraphicsMagick或ImageMagick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆