How I built a GhostScript Layer in AWS Lambda

Welcome to Ghostscript, a powerful tool for compressing, editing, and manipulating PDF files! With Ghostscript, you can easily build a Dockerfile from source to create the binary you need. Let us help you optimize your PDF files with Ghostscript’s robust features.

The Dockerfile

Whether you’re a developer looking to integrate Ghostscript into your application or a PDF enthusiast looking for a versatile tool, this Dockerfile will help you get up and running quickly. Let us show you how easy it is to build the binary.

FROM lambci/lambda-base-2:build
ENV GS_TAG=gs9540
ENV GS_VERSION=9.54.0

RUN yum install -y wget

RUN mkdir /usr/local/src/ghostscript && \
  cd /usr/local/src/ghostscript && \
  wget -qO - https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/$GS_TAG/ghostscript-$GS_VERSION.tar.gz | tar -zxf - && \
  cd ghostscript-$GS_VERSION && \
  ./configure --without-luratech && \
  make && make install
  
RUN echo "gs version is $(/usr/local/bin/gs --version)"

RUN cd /usr/local && \
  zip /tmp/ghostscript-9.54.0.zip bin/gs

This Dockerfile is specifically designed to compile Ghostscript from source, resulting in a binary that is packaged as a zip file. Although the compilation process may take some time, a convenient shell script is provided that will generate the zip file for you.

Publish the Layer

In the second section, this script will output the zip file.

#!/usr/bin/env bash

docker build -t gs-lambda-layer .
docker run --rm gs-lambda-layer cat /tmp/ghostscript-9.54.0.zip > ./ghostscript-9.54.0.zip

This script will publish the binary to AWS Layers, allowing you to easily share and reuse the Ghostscript Lambda layer in your Serverless applications.

#!/usr/bin/env bash
TARGET_REGION=eu-west-1
GHOSTSCRIPT_VERSION=9.54.0
LAYER_NAME='ghostscript'


LAYER_VERSION=$(
  aws lambda publish-layer-version --region "$TARGET_REGION" \
    --layer-name $LAYER_NAME \
    --zip-file fileb://ghostscript-9.54.0.zip \
    --compatible-runtimes python3.6 \
    --description "Ghostscript v${GHOSTSCRIPT_VERSION}" \
    --query Version \
    --output text
)

aws lambda add-layer-version-permission \
  --region "$TARGET_REGION" \
  --layer-name $LAYER_NAME \
  --statement-id sid1 \
  --action lambda:GetLayerVersion \
  --principal '*' \
  --query Statement \
  --output text \
  --version-number "$LAYER_VERSION"

Conclusion

In conclusion, with the script provided, you can easily publish the Ghostscript binary to AWS Layers and leverage its powerful features for PDF processing in your Serverless applications. If you’re interested in building a pipeline in Azure DevOps for Lambda functions, be sure to check out our related post. With this combined approach, you’ll have a robust and efficient workflow for PDF processing in your cloud-based applications. Don’t miss out on unlocking the full potential of Ghostscript for your PDF processing needs!

You Might Also Like
2 Comments
    • K'
      says:

      Hi, I didn’t use node on a Lambda but I think the runtime node 18.x exists. You only need to run a process to the attached binary. This layer will put it on /opt/bin/. Hope it’ll works for you!

Leave a Reply