Besides the shitty todo apps that are so conveniently demonstrated for all new technologies I wondered what the world of serverless would really be like.
This is part 1 of 3 of building a real world example using the serverless framework and how you can monetize your new infinitely scalable solutions.
In this part I talk about how to use serverless (the framework) to build an ffmpeg wrapper based on bucket events. The code for my open sourced serverless-ffmpeg can be found here.
What we want to do: Whenever a media file is uploaded to an s3 bucket, we want to post-process that into various formats using ffmpeg.
Why serverless: Serverless allows us to trigger a function once a file has been added/delete to a bucket, perfect for our post-processing action. Also we pay for the time we process, no more, no less. Last but not least, we can batch process and not worry about how many files are uploaded at once.
This is THE file where it all is tied together and if you’ve ever built anything bigger than a todo app, you know this will be a bottleneck — and it is. However! there are some fancy stuff you can do to make it a bit better, such as reference another file (see notes below) or use javascript to generate parts of the yml.
A couple of notes:
${file(./config.yml):source_bucket}
to grab values out of another yaml file. This is one way to abstract away the complexity of the serverless.yml and focus on a few parameters that may be important for deployment.We need webpack to bundle our javascript nicely, as well as use es6/7 features to write some modern day JS (rather than being bound to node 6.10 that lambda allows us). Let’s take a look at the webpack config + babelrc files to make that happen.
slsw.lib.entries
in order to auto generate entries based on our functions in serverless. Thanks to the smarts of the serverless-webpack lib.This is where serverless starts! As we specified in our serverless.yml
we want a function called main
to run from our handler.js
file, whenever there is an object created event in our source bucket.
Let’s take a look at some of the interesting parts of the app code to see how this is all tied together.
spawn
directly located in our project root!As part of any real world application unit testing is important. But how can we test serverless functions? We can unit test parts of them! But how do we unit test calling a binary to do things? We set up our testing infrastructure to support this!
In order for us to achieve this we’ll need ava (ava is great!), it allows us to quickly get started with a minimalistic configuration (see this package.json for how to configure it properly).
One of the great things about serverless is how easy it is to deploy and tear down. Since serverless framework does it all in a cloudformation templates it’s fast and easy. No more “what command did I run where”.
To deploy the whole stack, we run: sls deploy -v
. This will show the output of the cloudformation status so that we can make sure all resources are created as they should.
That’s it!
You can view this little video to see how it all works in the end.
serverless-ffmpeg_Recorded by kvaggelakos_asciinema.org
I hope you enjoyed this quick deep dive into serverless and a real world example! In the next parts we’ll be looking at more fun real world scenarios and how to monetize them, so stick around!
A couple of real life problems we encountered in this part:
In the next parts I’ll reveal more life scenarios and problems solved.
If you have any questions or comments please post them here and I’ll do my best to help.
One thing that wasn’t obvious to me when I started out was how important plugins were for the serverless framework. The two plugins we’ll be using in this part are (more plugins in part 2 and 3):
serverless-webpackBecause as of today aws lambda supports node 6.10, which does not fully support es6. We want that fancy es6/7 and some other nice packaging features.
serverless-s3-removerYou’ll quickly realize you’re deploying and tearing down your stack with the serverless framework many times as you’re testing things. If you’re working with buckets, this simply helps us remove the buckets and the content in them when we destroy our stack. (WARNING: You will loose content with this)