Up and running with cloud functions

den 30 mars 2017

What exactly is a cloud function? It’s a piece of code you run in a cloud environment that does computational work or a task. Think of it like any other function you normally write, but it resides and runs in the cloud. But what's the difference between a cloud function and other services like IaaS (Infrastructure as a Service) and PaaS (Platform as a Service)?

The big difference from IaaS and PaaS is that it lets you run code without provisioning anything and you only pay for the service when it does actual work. No need to think about scaling up/down or in/out as your function runs in parallel for each request. Think of it as an abstraction layer above PaaS, another word that is used for it is serverless architecture.

I first came in contact with cloud functions and AWS Lambda in particular when developing an app for Amazon Alexa with my team. I gave a little presentation about my brief dive into cloud functions a few weeks ago and thought I might as well share my findings and experiences here as a blog post.

Comparing Amazon, Microsoft and Google

All of these services work in the same way. They provide a platform where you upload executable code that will only be billed for the time and memory used while executing the function. All providers have free tiers, for instance, Amazon provides 1 million requests or 400 000GB/s for free, the other providers have similar free tiers.

How and when the code you uploaded will execute is up to you. All the services has built in triggers and events that works well with other services provided by either Amazon, Azure or Google, so it may be in your interest to ask yourself if the function will be used in conjunction with other provided services. For example, you can listen to events when a file is uploaded to Amazon S3 for Lambda and to Azure Blob Storage for Functions. More examples like these are:

  • Timer-based processing
    Example: Run a scheduled job in your function
  • Real-time file processing
    Example: Convert a video file or scale an image
  • Real-time stream processing
    Example: Process a real-time analytic stream
  • Event processing
    Example: HTTP event (WebHook), a file saved in Dropbox or OneDrive
  • Real-time bot processing
    Example: Handle interaction with Alexa or Cortan

This image taken from the AWS Lambda startpage shows a typical workflow scenario for a cloud function. In this particular case it's a Lambda function respondig to different events.

There are endless possibilities with this and both Amazon and Azure supports a variety of languages whereas Google only supports Node.js at the time being.

  • AWS Lambda runs JavaScript (Node.js), Python, Java and .NET Core
  • Azure Functions currently supports JavaScript, C#, F#, Python, PHP, Bash, Batch and PowerShell. What amazed me a bit is that by C# and F# support they mean the script versions of it. C# Scripting (.csx) and F# Scripting (.fsx) which doesn’t have great tooling support in Visual Studio. Again this is the current state of supported languages and it will surely be expanded over time, as a matter of fact, support for .NET Core was being worked on when this article was written.
  • Google Cloud Functions only supports Node.js

How to get started

My recommendation is to head over to Try Functions for Azure, Free trial for Google and Lambda for Amazon to try it out.

I’ve mostly tried Amazon and Azure, so I can give a few pointers on how they work. Azure and Lambda both have droves of examples you can play around with once you sign up. There are examples of events that I listed in the previous section in all the supported languages by the platform you choose. This was a great start for me to get a feel for how to chain the function together with existing services and to understand how it all worked besides looking at the documentation. You can edit the code in an editor directly in Amazon and azure to test and debug different events that you want your function to listen to.

Screenshot of how the web interface for a simple JavaScript WebHook looks like in Azure Functions
Once you’ve made yourself familiar with the respective web interfaces for Amazon and/or Azure, invest your time in how to deploy your code from a CLI (Command Line Interface) or IDE (Integrated Development Environment). Both Amazon and Azure has CLI and Visual Studio support for their functions. Azure has easy integration to push your code with different git integrations.

Great documentation can be found for Amazons Lambda functionality in their AWS CLI. A step I took to deploy my functions easily to Lambda was to create a gulp task following this implementation.

To scaffold templates I used Yeoman as a generator which have both Amazon and Azure template generators for different kinds of events and languages.

My impressions and conclusion

I had a fun time exploring the possibilities with cloud functions. It’s something new that all the big cloud providers provide a platform for. For me it was an easy way to quickly setup a WebHook for slack or a build trigger integration. As a developer setting up these functions for myself is both free and low effort. I can see the potential in using these functions as workers, processing real time data or events triggered by other services.

The tooling felt more robust with Amazon, working for both unix and window while Azure CLI tool was only working correctly on windows at the time of testing but I felt their easy git integration made up for it.

Give it a try! Start with looking at a few examples, try your own integration to get a feeling for how it works and how to deploy them from your local environment with IDE integrations or CLI commands. Happy cloud functioning.