Learning AWS Lambda: redv
I recently got intruiged by the serverless architecture. It basically allows you to run serverside code without the hassle of paying and maintaining for an actual server or VPS. It’s also much cheaper than all the other deployment options out there.
To get into it I started using up
by Apex, which is a simple framework to deploy serverless apps via AWS. It’s really amazing when you’re trying to set up simple servers and microservices: Write code, one command deploy, you’re live.
However, I needed something a little more complex, so I eventually moved to a custom setup. My plan was to create a Reddit Video (v.redd.it) downloader, because I recently saw that Reddit splits their videos into audio and video channels. So there’s basically no easy way to download them.
So my plan: Merge audio and video together using an AWS Lambda function running a ffmpeg
command. Writing the code for the actual conversion was fairly simple:
const ffmpeg = require('fluent-ffmpeg');
exports.handler = function(event, context, callback) {
ffmpeg()
.addInput(videoUrl)
.addInput(audioUrl)
.output('/tmp/video.mp4')
.on('error', err => {
context.done(null, err);
})
.on('end', () => {
// Upload video to S3 bucket...
})
.run();
};
It took some time until I had the ffmpeg
binary running smoothly inside the Lambda function, but after that things started to look really robust. However, this function, depending on the video, can take a few minutes to complete. Calling this via an API would just result in a timeout error 100% of the time. So, I had to set up a second Lambda function that invokes the convert function. I can then “ping” the S3 bucket from anywhere and check if the video has been converted. Relatively simple, but took quite bit of testing to get it right.
The frontend was more of a small chore then, because I was using Gatsby as a framework and Netlify for hosting. They are really a match made in heaven.
And here’s the final result: redv | Website that allows you to download v.redd.it videos easily
I’m pretty happy with it. It’s stable, bulletproof at least from what I can tell, and features state-of-the-art technology. Let’s see where this is going!