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

RUN yum install -y wget

RUN mkdir /usr/local/src/ghostscript && \
  cd /usr/local/src/ghostscript && \
  wget -qO -$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/ 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/ > ./

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

  aws lambda publish-layer-version --region "$TARGET_REGION" \
    --layer-name $LAYER_NAME \
    --zip-file fileb:// \
    --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"


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
    • K'

      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