Skip to content

Instantly share code, notes, and snippets.

@bensie
Last active November 20, 2023 10:13
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save bensie/56f51bc33d4a55e2fc9a to your computer and use it in GitHub Desktop.
Save bensie/56f51bc33d4a55e2fc9a to your computer and use it in GitHub Desktop.
ImageMagick Static Binaries for AWS Lambda
#!/usr/bin/env bash
# Must be run on an Amazon Linux AMI that matches AWS Lambda's runtime which can be found at:
# https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
#
# As of May 21, 2019, this is:
# Amazon Linux AMI 2018.03.0 (ami-0756fbca465a59a30)
#
# You need to prepend PATH with the folder containing these binaries in your Lambda function
# to ensure these newer binaries are used.
#
# In a NodeJS runtime, you would add something like the following to the top of
# your Lambda function file:
# process.env['PATH'] = process.env['LAMBDA_TASK_ROOT'] + '/imagemagick/bin:' + process.env['PATH']
#
# This works with both ImageMagick v6.x and v7.x
# version=6.9.10-23
version=7.0.8-45
sudo yum -y install libpng-devel libjpeg-devel libtiff-devel gcc
curl -O https://imagemagick.org/download/ImageMagick-$version.tar.gz
tar zxvf ImageMagick-$version.tar.gz
cd ImageMagick-$version
./configure --prefix=/var/task/imagemagick --enable-shared=no --enable-static=yes
make
sudo make install
tar zcvf ~/imagemagick.tgz /var/task/imagemagick/
@R-Harishkumar
Copy link

R-Harishkumar commented Apr 21, 2021

Where to Place this Code ?

  1. Update environment variables in your lambda function.
import os
os.environ["PATH"] = f"/opt/bin:{os.environ['PATH']}"
os.environ["LD_LIBRARY_PATH"] = f"/opt/lib:{os.environ['LD_LIBRARY_PATH']}"
os.environ["MAGICK_HOME"] = "/opt/"
os.environ["WAND_MAGICK_LIBRARY_SUFFIX"] = "-6.Q8"
os.environ["MAGICK_CONFIGURE_PATH"] = "/opt/etc/ImageMagick-6/"
os.environ["MAGICK_CODER_MODULE_PATH"] = "/opt/lib/ImageMagick-6.9.11/modules-Q8/coders/"

@samkit-jain
Copy link

@R-Harishkumar You may place the code at the top of the Python file.

@ClickheadZ
Copy link

@samkit-jain

I created the layer with your ZIP file and added it to my lambda, along with the code from step 16,
but when I try to do: from wand.image import Image
I get this error: Unable to import module 'lambda_function': No module named 'wand'

And if I try to package wand myself and add it then I get the error where wand doesn't find the shared library for ImageMagick.

Is there any other step you took to get Wand working in AWS Lambda?

@samkit-jain
Copy link

Hi @ClickheadZ Adding the ZIP file as a layer would give you the required libraries that you want at the OS level to communicate with imagemagick, gs, ... You still need the pip package files in your lambda archive. Did you add Wand as a dependency in your lambda function archive? Example here.

@ClickheadZ
Copy link

ClickheadZ commented Feb 24, 2022

@samkit-jain Thanks for the response! I had not added it properly, I repackaged and deployed a zip now and I'm getting a different error, not sure what I did wrong.

Here are the steps I took to package Wand:

  • Connect to an EC2 instance
  • pip install --target=package wand, pip install --target=package magickwand
  • scp the package to my computer, and zip the package along with my lambda function as per AWS zip deployment instructions

In lambda console, my lambda structure now looks like this:

myLambdaFunction
      - magickwand
      - magickwand-0.2.dist-info
      - wand
      - Wand-0.6.7.dist-info
      lambda_function.py

And when i try from wand.image import Image I now get the error message : Unable to import module 'lambda_function': MagickWand shared library not found.\nYou probably had not installed ImageMagick library despite the fact that I have deployed the imagemagick/ghostscript zip as a layer for this lambda and added it. Did I do something wrong in the Wand packaging or is the problem my lambda layer?

@divanshu-b
Copy link

@ClickheadZ Facing same issue, were you able to find a fix?

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