Skip to content

Instantly share code, notes, and snippets.

@basimhennawi
Last active May 24, 2022 19:20
Show Gist options
  • Save basimhennawi/21c39f9758b0b1cb5e0bd5ee08b5be58 to your computer and use it in GitHub Desktop.
Save basimhennawi/21c39f9758b0b1cb5e0bd5ee08b5be58 to your computer and use it in GitHub Desktop.
Graphicsmagick and Imagemagick static binaries for AWS Lambda
# Must be run on an Amazon Linux AMI that matches AWS Lambda's runtime
# As of Dec 6, 2018, this is Amazon Linux AMI – amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2
# Check latest from here: https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
# SSH to Amazon Linux AMI instance, that you just created:
ssh -i ${EC2_KEY} ${EC2_USERNAME}@${EC2_IP}
sudo yum -y install libpng-devel libjpeg-devel libtiff-devel gcc
# GraphicsMagick download latest stable as of Dec 6, 2018, this is 1.3.31 (latest stable)
curl -O https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.31/GraphicsMagick-1.3.31.tar.gz
tar zxvf GraphicsMagick-1.3.31.tar.gz
cd GraphicsMagick-1.3.31
./configure --prefix=/var/task/graphicsmagick --enable-shared=no --enable-static=yes
make
sudo make install
tar zcvf ~/graphicsmagick.tgz /var/task/graphicsmagick/
# Now you need to copy this tar file locally
scp -i ${EC2_KEY} ${EC2_USERNAME}@${EC2_IP}:imagemagick.tgz .
# Extract it and put it in your lambda function root to be packaged, zipped and deployed
# Assuming imagemagick dir is at the root of the lambda function, add the following:
const BIN_PATH = process.env['LAMBDA_TASK_ROOT'] + "/graphicsmagick/bin/";
const Gm = require('gm').subClass({ appPath: BIN_PATH });
# And inside the handler:
process.env['PATH'] = process.env['PATH'] + ':' + BIN_PATH;
# Must be run on an Amazon Linux AMI that matches AWS Lambda's runtime
# As of Dec 6, 2018, this is Amazon Linux AMI – amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2
# Check latest from here: https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
#
# Lambda includes ImageMagick 6.7.8-9 preinstalled, so you need to prepend PATH
# with the folder containing these binaries in your Lambda function to ensure
# these newer binaries are used.
# SSH to Amazon Linux AMI instance, that you just created:
ssh -i ${EC2_KEY} ${EC2_USERNAME}@${EC2_IP}
sudo yum -y install libpng-devel libjpeg-devel libtiff-devel gcc
curl -O http://www.imagemagick.org/download/ImageMagick.tar.gz
tar zxvf ImageMagick.tar.gz
cd ImageMagick
./configure --prefix=/var/task/imagemagick --enable-shared=no --enable-static=yes
make
sudo make install
tar zcvf ~/imagemagick.tgz /var/task/imagemagick/
# Now you need to copy this tar file locally
scp -i ${EC2_KEY} ${EC2_USERNAME}@${EC2_IP}:imagemagick.tgz .
# Extract it and put it in your lambda function root to be packaged, zipped and deployed
# Assuming imagemagick dir is at the root of the lambda function, add the following:
const BIN_PATH = process.env['LAMBDA_TASK_ROOT'] + "/graphicsmagick/bin/";
const Gm = require('gm').subClass({ appPath: BIN_PATH });
# And inside the handler:
process.env['PATH'] = process.env['PATH'] + ':' + BIN_PATH;
@joshuakwaite
Copy link

joshuakwaite commented Mar 14, 2019

The link for downloading GraphicsMagick zipped was broken for me (it is a redirect link), so I went to the source and got this link which worked (but I had to unzip it with a different command, tar xf ) - https://versaweb.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.31/GraphicsMagick-1.3.31.tar.xz

@jp928
Copy link

jp928 commented Jun 1, 2019

@joshuakwaite
Thanks for your comment. In my case, I can't get permission to execute GraphicsMagick from /var/task.
I copied to /tmp and give it permission 755 as mentioned: https://stackoverflow.com/questions/27708573/aws-lambda-making-video-thumbnails/29001078#29001078
Do you mind to share how did you overcome this issue?

@joshuakwaite
Copy link

joshuakwaite commented Jun 3, 2019

@jp928 I was never able to actually get this to run. I got as far as successfully building and extracting the binaries, but still couldn't get it to work inside Lambda.

@guzz
Copy link

guzz commented Jun 12, 2019

Did anyone managed to make this work?

@jp928
Copy link

jp928 commented Jun 13, 2019

@guzz
The binary mentioned in this post works.

@php-wizard
Copy link

php-wizard commented May 1, 2020

Hello,
i have tried launching this binary for converting from PNG => JPG , changing also the colorspace to 16-bit RGB, but it gives me an error saying it requires some config file.


./gm convert ./logo_xs.png -colorspace RGB -colors 65535 ./logo_xs.jpg
./gm convert: Unable to access configuration file (delegates.mgk) [No such file or directory].

I removed the lib folder, that may be the reason.
Does the program require the lib folder to be in the same folder as the executable?
I thought it was completely stand-alone

EDIT: i will try this solution https://superuser.com/questions/428553/install-compiled-binary-in-non-standard-environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment