Creating serverless one-shot containers in Azure
Containers aren’t just useful for continuously running processes or applications. There’s also the option of using them for short-lived or one-shot processes. Combine that with serverless Azure Functions, and you get the absolute best of both containers and serverless.
In this article I’ll talk about my experience with setting up serverless, one-shot containers as a solution to a problem I was trying to solve in a personal project of mine.
I’m currently working on a personal project that, at its core, allows users to manage and edit audio files in a web application.
Part of the functionality is that, after uploading, the system generates a waveform for the user to interact with in their browser. This way they can more easily navigate their files.
After doing some research on how to generate waveforms, I came across https://github.com/bbc/audiowaveform, which is a C++ program that can generate waveforms in different formats. In my case, being able to generate JSON was exactly what I was looking for.
To actually be able to use the program though, I would obviously need to run it. I could just spin up a virtual machine, but then there’s the issue that I only need to process some files periodically. I only want to process each file once, and that’s when it’s uploaded.
Because I don’t need to be able to process files all the time, I really don’t want to keep a server running. That’s just wasted money and electricity. At the same time, I want to make sure that my system can handle when I get a sudden large influx of files that need to be processed,
The solution? Containers and Azure Functions!
Containers are ideal here, since I can run them as ‘one-shot’ processes. And I’m able to completely determine everything about how my container is configured. Including installing a C++ program on it.
Once I have my image, I can start a container on-demand with a few parameters, make it process a single audio file using a startup script, and then upload the resulting waveform back to where ever I want to store it. Once it has finished what it needs to do, it simply shuts down. Next time I call it, it does the exact same thing all over again.
Because my container isn’t running when I don’t need it, and there is no persistent data storage connected to it, costs are kept to a minimum. I only pay for the time the container is actually doing things, instead of continuously keeping a VM running (and paying for it).
To make things even easier I can trigger my container using Azure Functions.
Using an Azure Function that runs Powershell to start a new instance of the container I created before, allows me to have all the advantages of working with containers, plus all the advantages of serverless:
- We’re only paying for the processing power we are actually using.
- We’re creating a much more scalable system, because the function will create new instances of the container each time the function is executed.
Are serverless containers for you?
All I can say is that I love having the control that containers give me, combined with the scalability, flexibility and pricing model of serverless. It’s a win-win situation, and I seriously suggest you check it out.
If you’re interested in seeing some actual code, I uploaded an example of the container and startup script I’m using on my Github: https://github.com/nstubbe/Generate-waveform-container
You can find the PowerShell command that my function runs in the readme.
Thanks for reading
I hope you enjoyed this article. If you have any questions, remarks or feedback, feel free to reach out to me via firstname.lastname@example.org.
If you would to stay up-to-date with the articles I publish, please consider subscribing to my newsletter via the form below.