How to Build Lightning Bolts With Azure Functions
Have you heard of microservices? Tiny tidbits of code living in the cloud, just waiting to do your bidding? Have you started researching them and become discouraged by the glut of technical information and the dearth of common-sense how-to advice? Better yet, have you investigated which microservice hosting platform is right for you? Docker? Web API? Home-grown? There are plenty of options for hosting your microservices, but most have you starting from scratch.
If you are interested in applying your current knowledge base and avoiding the steep learning curve of starting at square one with microservices, then Azure Functions may be just the ticket for you.
It is as simple as writing your function in your preferred language and deploying it to the cloud. If you are using an HTTP request trigger, Azure provides you a handy URL for your calls.
So, that’s the 30,000-foot view. How do you get started building and consuming these services?
The rest of this article will walk you through the process step by step. At the end, you will have a fully functional Azure Function lightning bolt living in the cloud, waiting to wreak havoc on the earth.
Get Set Up for Azure
The first step is to get set up with your very own Azure service account. Here are the steps to get set up:
- Visit the Azure site at http://functions.Azure.com.
- Either log in to an existing Microsoft Account or create a new one.
- Select the Azure subscription you will be using for hosting your functions.
- Give your function application a new name.
- Select the server region you would like to use to host your application. Selecting a region near your users or yourself is the best option.
- Click the “Create + get started” button.
The process to create your Azure Function app will run and stub out what you need to get started. This only takes a couple seconds, and then you will be ready to start coding.
When you are presented with the Scenario selection screen for your app, it’s time to code your function.
Figure 1: Azure Functions Start Screen
Coding Your Azure Function
Azure has some easy templates to get you started. This example will use a Webhook to initiate the Azure Function you create. This means you can use a web browser with your unique URL and parameters to call your app.
You will get a walkthrough of the features of the interface. Click through and read the pop-up tips to get a high-level understanding of the features in the interface. After you’re done with the tour, you will land in a code editor already pre-populated with code for your App.
Figure 2: Pre-populated Code Editor
Next up is modifying the code to implement your function. My example implements a dice roll simulator. It allows the user to supply three parameters: the number of sides on the die to roll, the number of times to roll the die, and a positive or negative integer to modify the total of the roll result.
This example doesn’t use anything beyond the core functionality of .Net, so a single using statement is all that’s necessary.
Now contain your function in a single method named Run.
This method takes two parameters: an HttpRequestMessage req that contains input from a URL query string or HTTP post, and a TraceWriter log for writing to the system log. A Tracewriter log is encouraged over writing to the console as Azure functions are not persistent. They spin up, do their work, and then evaporate. As such, anything written to the console is no longer accessible after function execution, whereas a TraceWriter log persists and is available for your review.
Your function will parse the req parameter for the values passed into it to simulate the roll of the die. But first, write a log entry to indicate a successful call to the Azure function.
You now have the values input into your Azure function in variables ripe for you to consume. Next, create a Random number generator to get the result of rolling your dice.
Your function has now generated a random number that simulates you rolling a die with a specified number of sides a specified number of times and then adding a modifier to the result. You are now ready to return that result to the system that made the call to your function.
Now that the code is entered, save and run it to activate your Azure Function and make it ready to test and use.
Test Your Azure Function
Azure functions have an extremely easy-to-use interface for testing applications that are called via Webhooks. Above your code editor you will see a URL. Copy that URL and paste it into your favorite browser bar.
It should resemble:
The value xxxxx will be your unique application key. You will need to add your parameters to the URL. In this case, we will add the dice parameters in the form &sides=6&rolls=1&modifier=0.
This will simulate one roll of a six-sided die with no modifier.
Once you’ve entered the URL into the browser bar, press the enter key to call the application. The web browser will display the result. In this case, it looks like:
Under the code editor window, you will also see the results of the call in your log history.
There you have it. A fully implemented Azure Function ready for you to call at any point you need it to perform whatever work you need it to do. Your own personal lightning bolt ready to call down from the Azure Cloud.
Where Do I Use It?
So now that you have this new power at your fingertips, what do you do with it? The potential can be a bit intimidating, but with a bit of creative thought there are plenty of scenarios where this platform may be useful in your applications.
One of the largest benefits of working with Azure Functions is that they scale automatically to the size you need without any additional configuration. This makes them perfect for use as back-end processing logic for mobile and web applications.
There are no usage charges for Azure Functions until they are put to use calculating, simulating, processing, storing, and retrieving data.
You can configure an Azure Function to be triggered automatically when you load files into an Azure Blob Storage. The function will then process those files for whatever need you have, be it reporting, transformation, or database storage. This is perfect for applications that process and transform client data.
You can base code execution on timers, so that at intervals you’ve specified, functions execute the tasks you need them to. Code that fires at predetermined intervals is a great way to implement periodic checks of service availability, or to carry out clean-up processes on a regular basis.
Beyond these scenarios, Azure Functions can be triggered by HTTP calls as demonstrated earlier, integrated with external SaaS services such as DropBox and OneDrive, or with Stream processing for servicing calls from IoT devices.
Here are just a few examples of how this platform could be used in real-world products:
IoT Traffic Counter Application
Imagine an IoT device with a camera that calls an Azure function to count people or cars passing by.
All of this traffic data is uploaded via the IoT device stream to a central database with location, time, and any other meta-data you want to capture. Then, reporting is connected and, in real time, you see a heat map of road conditions, customers in stores, or event attendance.
With the low cost of IoT device infrastructure, you can bundle this up and be selling a fully functioning service to customers in no time.
QR Code Context Sensitive Help
Another example could be a visitor or tourist bureau that places QR codes based on locations or services linked to Azure Function Webhooks.
These QR codes could provide historical information, answers to frequently asked questions, or integration to real-time call centers to help visitors/customers with anything they need.
With billions of smart phones in pockets the world over, the potential audience is staggering. QR help access could either be a paid service that organizations subscribe to, or it could be funded from context-sensitive ads based on the information presented by the customer and their current geographical location.
Just for Fun… QR Code Tag
This one would simply need a web page for registration and authentication (both possible with Azure Functions), a QR code generated for each “Player,” and a scoreboard to track points and leaders.
Players would display their QR codes for scanning by other players, which would link to a URL that would record the “Tag” and keep score, all with Azure Functions.
There are team building and social networking opportunities from a game like this.
And many more applications… The possibilities are countless.
If you’re inspired by the possibilities microservices have to offer and want to dig deeper into Azure Functions, Bob Familiar’s book Microservices, IoT, and Azure: Leveraging DevOps and Microservice Architecture to deliver SaaS Solutions is a great starting point.
Revolutionize Your Infrastructure With Azure Functions
So, can you see the potential? How valuable to you is the ability to have massively scalable computing power callable from anywhere, at any time, to do whatever you need?
When you implement Azure Functions, you remove the headache of constant focus on scalability. Instead, you can focus on delivering fantastic features and experiences for your users in your applications.
The back end and infrastructure is taken care of for you. You are free to focus on what you do best: writing those lightning bolts of code.
Azure Functions are the ultimate back-end development solution for programmers. Build and deploy your lightning bolts and then call them down from the cloud when you need them.
The next step for you is to roll up your sleeves and get elbow-deep in this technology. Head over to http://functions.azure.com and start building. You’ll be surprised at how easy it is to get started and how quickly you can have functional applications ready to go.
If you have questions or run into something that has you stumped, I’ve setup a Slack team to help support developers interested in getting started with Azure Functions. Request your invite at http://natthompson.com/join-me-on-slack and join the conversation.
Now get coding!