FFmpeg, SOX, Pandoc and RSVG for AWS Lambda
You can now use all the power of FFmpeg, SOX, Pandoc and RSVG to manipulate video, sound files, SVG images and text documents in Lambda functions, with just a few lines of code. We’ve pre-packaged four commonly used file conversion utilities into Lambda layers, which you can use with any serverless framework or deployment utility.
With low on-demand cost and scalability, cloud functions are ideal for file conversions. But for computationally intensive tasks, such as transcoding video, compiled code still rocks, and in most cases the best way of converting files is to just call into a standard Unix utility such as FFmpeg. The basic AWS Lambda container is quite constrained, and until recently it was relatively difficult to include additional binaries into Lambda functions. Lambda Layers make that easy.
A Layer is a common piece of code that is attached to your Lambda runtime in the
And the best part of this is that you can also use Layers published by other people. Here are the four common Unix utility layers: you can build and deploy your own versions easily from Github, or just use the Layers we published directly:
/opt/bin/ffprobe(Source on GitHub).
/opt/bin/pandoc(Source on GitHub)
/opt/bin/rsvg-convert(Source on GitHub)
/opt/bin/soxi(Source on GitHub)
The layers are published according to the original licenses from the Unix utilities, GPL2. For more information on those binaries and how to use them, check out the original project pages: https://ffmpeg.org/, http://pandoc.org, http://sox.sourceforge.net and https://wiki.gnome.org/Projects/LibRsvg.
For some nice examples of these layers in action, check out these projects:
- Serverless Video Thumbnail Builder using AWS SAM and FFMpeg
- SVG to PDF converter using Claudia.js and RSVG
- Markdown to DOCX converter using Claudia.js and Pandoc
- Markdown to DOCX converter using AWS SAM and Pandoc
You can also use some ready-made components in the Serverless Application Repository that convert files using S3 bucket uploads:
How to use Layers in your applications
You can easily attach these layers to your functions using CloudFormation. Just include the
Layers property into
ConvertFileFunction: Type: AWS::Lambda::Function Properties: Handler: index.handler Runtime: nodejs8.10 CodeUri: src/ Layers: - !Ref LambdaLayerArn
With AWS SAM, you can also use the
ConvertFileFunction: Type: AWS::Serverless::Function Properties: Handler: index.handler Runtime: nodejs8.10 CodeUri: src/ Layers: - !Ref LambdaLayerArn
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.