FFmpeg, ImageMagick, Pandoc and RSVG for AWS Lambda

Gojko Adzic in Serverless 2 minutes Last updated Jun 20, 2019

Update: 20 June 2019 - new versions of layers for Amazon Linux 2, all layers published to SAR

Lambda runtimes based on Amazon Linux 2 come without almost any system libraries and utilities. Using the additional layers listed in this post, you can add FFmpeg, ImageMagick, Pandoc and RSVG to your Lambda environments, and manipulate video, sound files, images and text documents in Lambda functions, with just a few lines of code. The layers are compatible with Amazon Linux 1 and Amazon Linux 2 instances (including the nodejs10.x runtime, and the updated 2018.03 Amazon Linux 1 runtimes).

A Lambda Layer is a common piece of code that is attached to your Lambda runtime in the /opt directory. You can reuse it in many functions, and deploy it only once. Individual functions do not need to include the layer code in their deployment packages, which means that the resulting functions are smaller and deploy faster.

We published these layers to the AWS Serverless Application Repository, so you can install them with a single click into your AWS account. For manual deployments and to configure versions, check out the individual GitHub repositories.

  • image-magick-lambda-layer: installs /opt/bin/convert, /opt/bin/mogrify and similar tools
  • ffmpeg-lambda-layer: installs /opt/bin/ffpmeg and /opt/bin/ffprobe
  • pandoc-lambda-layer: installs /opt/bin/pandoc
  • rsvg-convert-lambda-layer: installs /opt/bin/rsvg-convert

The layers are published according to the original licenses from the Unix utilities, GPL2 or later. For more information on those binaries and how to use them, check out the original project pages: https://ffmpeg.org/, http://pandoc.org, https://imagemagick.org and https://wiki.gnome.org/Projects/LibRsvg.

How to use layers in your applications

Click on individual GitHub repository links to see example usage code in action. Here are a few code snippets for quick access:

Using SAM, you can deploy the layer and a function from the same template:

ImageMagick:
  Type: AWS::Serverless::Application
  Properties:
    Location:
      ApplicationId: arn:aws:serverlessrepo:us-east-1:145266761615:applications/image-magick-lambda-layer
      SemanticVersion: 1.0.0
ConvertFileFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: image-conversion/
    Handler: index.handler
    Runtime: nodejs10.x
    Layers:
      - !GetAtt ImageMagick.Outputs.LayerVersion

Without SAM, deploy a layer using the application links above, then just include the Layers property into AWS::Lambda::Function

ConvertFileFunction:
  Type: AWS::Lambda::Function
  Properties:
    Handler: index.handler
    Runtime: nodejs8.10
    CodeUri: src/
    Layers:
      - !Ref LambdaLayerArn

With claudia, use the --layers <LambdaLayerArn> option with claudia create or claudia update to attach a layer to a function.

With the Serverless Framework, use the Layers property to link a layer to your service.