So you want to use Magick.NET on AWS Lambda? Well, not as simple as it sounds. Fortunatelly for you, here is a step by step guide to use Magick.NET on AWS Lambda.
Create an Amazon Linux instance used for building.
Preferably choose some bigger instance so the build process is quick. (I used
c4.xlarge). You can create it as a spot instance to save some coins, after the process
is done, you won't need the instance anymore. Choose more storage than the default 8 GB,
you can run out of inodes if you have a lot of files (actually happened to me). I used
20 GB and everything was smooth.
After the instance is created, ssh into it and login as root (sudo su -).
You can use this "one-liner" to compile it all, it's basically all of the commands below merged into one command. The downside is that in case of error it gets harder to identify the exact step that failed. If you don't want to use this, skip below and do the step-by-step guide.
yum update -y && yum groupinstall "Development Tools" -y && yum install -y cmake git make autoconf yum-utils libunwind libicu wget nasm && export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/ && git clone https://github.com/dlemstra/Magick.NET.git && cd Magick.NET && git checkout tags/7.2.1.0 && cd ImageMagick/Source && ./Checkout.sh && cd ImageMagick/ImageMagick && git clone https://github.com/libjpeg-turbo/libjpeg-turbo.git jpeg && cd jpeg && git checkout tags/1.5.2 && autoreconf -fiv && ./configure --disable-shared --with-pic --prefix="/usr/local/" && make && make install && cd .. && git clone https://github.com/madler/zlib.git zlib && cd zlib && git checkout tags/v1.2.9 && ./configure --static && make CFLAGS='-fPIC' && make install && cd .. && git clone https://github.com/mm2/Little-CMS.git lcms && cd lcms && git checkout tags/lcms2.8 && ./configure --disable-shared --with-pic && make && make install && cd .. && git clone https://github.com/webmproject/libwebp.git webp && cd webp && git checkout tags/v0.5.1 && ./autogen.sh && ./configure --disable-shared --with-pic && make && make install && cd .. && git clone https://git.code.sf.net/p/libpng/code png && cd png && git checkout tags/v1.6.13beta03 && ./autogen.sh && ./configure --disable-shared --with-pic && make && make install && cd .. && wget https://download.savannah.gnu.org/releases/freetype/freetype-2.7.tar.gz && tar xzf freetype-2.7.tar.gz && mv freetype-2.7 freetype && cd freetype && ./configure --with-pic && make && make install && cd .. && ./configure --with-quantum-depth=8 --enable-hdri=no --with-magick-plus-plus=no --with-jpeg --with-png --with-webp --with-lcms --with-zlib --without-pango --without-x --without-fontconfig --with-freetype --disable-shared --with-pic --enable-delegate-build && make && make install && cd ../../../../Source/Magick.NET.Native && wget https://gist.githubusercontent.com/RikudouSage/8c75d3c9eb6f9d49e1b932e7089a7946/raw/d175a8843d1369477ac4e8d3a739ee8ef6489717/CMakeLists.txt && mkdir build && cd build && cmake .. -DQUANTUM_DEPTH=8 -DHDRI=no && make && mkdir -p ../../../../Magick.NET.Native/{lib,lib64} && cp *.so ../../../../Magick.NET.Native/lib && cd ../../../../Magick.NET.Native && cp /usr/lib64/libfreetype.so.6 ./lib64 && zip -r Magick.NET.Native.zip $(ls)
After this command finishes, you can skip to the Done section.
- Update packages
yum update -y
- Install development tools
yum groupinstall "Development Tools" -yyum install -y cmake git make autoconf yum-utils libunwind libicu wget nasm
- Fix package config resolution (This needs to be done every time you log out and in)
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
- Clone Magick.NET and cd into it
git clone https://github.com/dlemstra/Magick.NET.gitcd Magick.NET
- Select the
Magick.NETversion you want and checkout the tag, I'll use 7.2.1.0git checkout tags/7.2.1.0
- Clone the coresponding
ImageMagickversioncd ImageMagick/Source./Checkout.sh
- Build
ImageMagickdependenciescd ImageMagick/ImageMagick
- Build
jpeggit clone https://github.com/libjpeg-turbo/libjpeg-turbo.git jpegcd jpeggit checkout tags/1.5.2autoreconf -fiv./configure --disable-shared --with-pic --prefix="/usr/local/"make && make installcd ..
- Build
zlibgit clone https://github.com/madler/zlib.git zlibcd zlibgit checkout tags/v1.2.9./configure --staticmake CFLAGS='-fPIC' && make installcd ..
- Build
lcmsgit clone https://github.com/mm2/Little-CMS.git lcmscd lcmsgit checkout tags/lcms2.8./configure --disable-shared --with-picmake && make installcd ..
- Build
webpgit clone https://github.com/webmproject/libwebp.git webpcd webpgit checkout tags/v0.5.1./autogen.sh./configure --disable-shared --with-picmake && make installcd ..
- Build
pnggit clone https://git.code.sf.net/p/libpng/code pngcd pnggit checkout tags/v1.6.13beta03./autogen.sh./configure --disable-shared --with-picmake && make installcd ..
- Build
freetypewget https://download.savannah.gnu.org/releases/freetype/freetype-2.7.tar.gztar xzf freetype-2.7.tar.gzmv freetype-2.7 freetypecd freetype./configure --with-picmake && make installcd ..
- Build
ImageMagick, you can replace quantum-depth and hdri parameters./configure --with-quantum-depth=8 --enable-hdri=no --with-magick-plus-plus=no --with-jpeg --with-png --with-webp --with-lcms --with-zlib --without-pango --without-x --without-fontconfig --with-freetype --disable-shared --with-pic --enable-delegate-buildmake && make install
- Create
Magick.NETwrapper, in thecmakestep input the same settings for quantum-depth and hdri as inImageMagickcompilationcd ../../../../Source/Magick.NET.Nativenano CMakeLists.txtand paste (CTRL+SHIFT+V) the content of this file.mkdir build && cd buildcmake .. -DQUANTUM_DEPTH=8 -DHDRI=nomake
- Wrap the libraries in a nice package
mkdir -p ../../../../Magick.NET.Native/{lib,lib64}cp *.so ../../../../Magick.NET.Native/libcd ../../../../Magick.NET.Nativecp /usr/lib64/libfreetype.so.6 ./lib64zip -r Magick.NET.Native.zip $(ls)
Now you can move the Magick.Net.Native.zip file somewhere you like,
for example move it to /home/ec2-user and then copy it to your pc via
SFTP.
In your C# project for AWS Lambda extract the zip archive and everything's done.
Your project should include two new directories, lib and lib64.
You must set the project to include these two directories in publish package.