The second step in my cloud journey is to discover how to use ‘serverless’ computing resources. New technologies such as AWS Lambda and Azure Functions make it easy to run arbitrary code across a scalable VM workforce without having to provision or manage those servers yourself.
This week I’ve picked Azure Functions to play with. And because I’m a Python fan – I decided to try and write a function in Python with HTTP triggers and responses. The end game in my mind is to create a microservice-style API built upon Azure Functions.
This wasn’t quite as simple as I’d expected, but once I worked out the concepts for data input and output (hint: environmental variables) it got a lot easier. It was one of those things where I’m pretty sure not many other people have walked the same path. There aren’t really any examples I could fine online of people doing this in Python (but the docs are great for Node.js and C#). Thankfully, my quest was made much easier by Christopher Anderson (the Azure Functions Product Manager himself!!) who made himself available via Twitter.
For those of you who are looking to achieve something similar, I’ve created a helper class and example code and put it on GitHub. Check it out.
This sample code will allow you to do the following:
- Receive GET (Query String) data
- Receive POST data
- Send HTTP Response Status Codes
- Send body response data
- Send arbitrary HTTP response headers
- View all system environmental variables
To get started quickly:
- Login to the Azure Portal
- Create a new function
- Link it to this Git repository
- Test the endpoint (https://myexamplefunc.azurewebsites.net/api/myfunction1)
- Look at the data logged in the Portal’s live feed
When you want to start editing and writing code yourself, feel free to fork my sample code. You can deploy via Git, or one of the other repositories offered in Azure:
For now all this is still a bit buggy (hence the “preview” label), but it does seem to work pretty well. Better support for Python is on the radar, so watch this space!