How to use gifsicle in AWS lambda

305 views Asked by At

Is there a way to use gifsicle in AWS lambda? I know there is a package called pygifsicle, but it seems it requires the gifsicle version of AWS Linux 2?

I don't see a binary built for RedHat version of gifsicle

So my questions are,

Do I need to build one for AWS Linux 2 to use it along with pygifsicle? Even if I build gifsicle for AWS Linux 2, how to use it along with pygifsicle?

3

There are 3 answers

0
samtoddler On BEST ANSWER

As I read the documentation you can build one binary for Building Gifsicle on UNIX and can package that with your lambda zip file which can be called as a normal command in lambda function.

Like it is being called in the pygifsicle

subprocess.call(["gifsicle", *options, *sources, "--colors",
                str(colors), "--output", destination])
0
Osama Bin Saleem On

My Dockerfile where I'm building it from the source.

FROM public.ecr.aws/lambda/python:3.8-arm64

RUN yum -y install install make gcc wget gzip

RUN wget https://www.lcdf.org/gifsicle/gifsicle-1.93.tar.gz
RUN tar -xzf gifsicle-1.93.tar.gz
RUN cd gifsicle-1.93 && \
    ./configure && \
    make && \
    make install

COPY requirements.txt ./
RUN yum update -y && \
    pip install -r requirements.txt

COPY . .
CMD ["app.handler"]
0
MartinG On

The current selected answer may be correct in general, but here is an in-depth process that will be 100% working on your lambda environment. Let's start:

Determine your Lambda Runtime by checking the 1st and the 4th column of the table in the link. For example my python3.9 uses Amazon Linux 2 - BE SURE TO CHECK if its Amazon Linux or Amazon Linux 2 (the 2 matters a lot and I lost 2 hours only to find I missed this step). One error you will encounter if you miss this step is "/lib64/libc.so.6: version `GLIBC_2.32' not found" because of different runtimes - during build of gifsicle and lambda runtime.

Go to EC2 and Launch an Instance with the same OS - make sure you are selecting the right version - Amazon Linux or Amazon Linux 2, etc.

Log in the instance via SSH and do the following commands:

yum -y install gcc

wget https://www.lcdf.org/gifsicle/gifsicle-1.93.tar.gz

tar -xzf gifsicle-1.93.tar.gz

cd gifsicle-1.93

Now you can build gifsicle, but you may want to exclude some of its features - giffdiff or gifview - check docs here if you need

In my case I didn't need any of them so I did:

./configure --disable-gifview --disable-gifdiff

make

sudo make install

Now prepare the lambda layer:

mkdir gifsicle

cd gifsicle

mkdir bin

cd bin

cp /usr/local/bin/gifsicle .

cd ..

zip -r ../gifsicle.zip .

Download the zip file and use it to create a new lambda layer. Attach it and you're done.

Don't forget to turn OFF your EC2 instance as its no longer needed.