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;
@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