Articles

How to create a static SOX executable to invoke it in AWS Lambda


SOX is an awesome tool that can transcode audio files in many different formats. But if you want to use SOX inside an AWS Lambda Container you should generate a binary that includes all the code needed to run in an isolated environment, because you won't have some needed libraries preinstalled (like lame and libmad).

This is really useful if you can't afford the Amazon Elastic Transcoder service and need a cheaper solution instead.

A great application for this is to transcode your Asterisk Monitor recordings to MP3 in a server-less way.

Setup a new Amazon Linux EC2 Instance

Create an Amazon Linux EC2 instance, and install some basic packages needed to compile the rest of the stuff

Build a static libgsm

libgsm is a library used to encode and decode GSM audio files.

Build a static libmad

libmad is an MP3 library to encode and decode MPEG-1 and MPEG-2 formats.

Build a static lame

LAME is a high quality MPEG Audio Layer III (MP3) encoder.

Build a static SOX binary

Once all the above libraries are installed, we can now proceed to build a static SOX binary.

LAME is a high quality MPEG Audio Layer III (MP3) encoder.

Invoking SOX inside a Lambda container

Once you have generated the binary file, create an empty directory and copy the sox binary there. Add a file like this one so you can try it in Lambda, name it index.js:

Pack everything in a zip file

Create your Lambda and invoke SOX

Go to your Lambda console and create a new Lambda, choose NodeJS 4.3 as the runtime. Use the created ZIP file as the source of your code and upload it.

You can now go to your Lambda function and hit the 'Test' button. In the output console you should be able to see the output of your SOX binary :)

Enjoy!