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" -y
yum 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.git
cd Magick.NET
- Select the
Magick.NET
version you want and checkout the tag, I'll use 7.2.1.0git checkout tags/7.2.1.0
- Clone the coresponding
ImageMagick
versioncd ImageMagick/Source
./Checkout.sh
- Build
ImageMagick
dependenciescd ImageMagick/ImageMagick
- Build
jpeg
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 ..
- Build
zlib
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 ..
- Build
lcms
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 ..
- Build
webp
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 ..
- Build
png
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 ..
- Build
freetype
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 ..
- 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-build
make && make install
- Create
Magick.NET
wrapper, in thecmake
step input the same settings for quantum-depth and hdri as inImageMagick
compilationcd ../../../../Source/Magick.NET.Native
nano CMakeLists.txt
and paste (CTRL+SHIFT+V
) the content of this file.mkdir build && cd build
cmake .. -DQUANTUM_DEPTH=8 -DHDRI=no
make
- Wrap the libraries in a nice package
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)
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.