Serverless in Context
Getting the Most From Azure Functions
*** A newer, updated version of this course is available here. ***
An Introduction to Azure Functions
Serverless Computing has emerged as a capable and low-friction means to execute custom logic in the public cloud. Whether you're using Amazon Lambda, Google Cloud Functions, or Azure Functions, you have a wide variety of target languages, ecosystem integrations, and deployment mechanisms to choose from. All this while leaving the heavy lifting of server provisioning and maintenance to the experts, which gives you plenty of time to focus on your differentiated application functionality.
In this "Introduction to Azure Functions" course, you’ll learn how to build Azure Function applications in the cloud. You'll discover the core feature set of Functions and see how to integrate with a variety of sibling Azure services. You'll explore Function topics like security, monitoring, deployment, and testing best practices. You'll also learn about ideal Functions use cases and the pricing model. Finally, you'll learn about how we've arrived at the serverless computing model, and where serverless is likely to go in the future. By the end of this course, you’ll have a solid foundation to continue exploring Functions on your own, and incorporating Azure Functions capability into your work.
An Introduction to Azure Functions: What You'll Learn
|Lecture||What you'll learn|
|Intro||What to expect from this course|
|Serverless Computing In Context||Understanding what serverless computing is, and how we got here|
|Core Features||A high-level overview of what Azure Functions is, and its basic capabilities|
|Creating Your First Function||A demo of creating your first function in the Azure portal|
|Security||A review of security features in Azure Functions|
|Using API Key Management||A demo of configuring an Azure Function to require API key use|
|HTTP Proxies||A discussion of lightweight HTTP Proxy support|
|Proxying Azure Blob Storage||A demo of using Functions' HTTP Proxy support to front Azure blob storage|
|Triggers and Bindings||Event-based triggering of functions and declarative binding of inputs and outputs|
|Triggering on Queues and Binding to DocumentDB||A demo of Triggering with Azure Queues and Binding Function Output to DocumentDB|
|Testing and Debugging||Tools and techniques for working with Functions during the development cycle|
|Deployment||Options for deploying Azure Function apps into production|
|Deploying From a Local Git Repo||A demo of deploying a complete Azure Function app to the cloud, from a local Git repository|
|Monitoring||Tools for monitoring Azure Functions during dev, test, and release|
|Use Cases||A discussion of ideal use cases for serverless compute and Azure Functions|
|Pricing||A review of how Functions are priced, and a demo of determining price using the Azure Pricing Calculator|
|Serverless in the Future||A short discussion on the future of serverless in the cloud|
|Summary||Course wrap up|
If you have thoughts or suggestions for this course, please contact Cloud Academy at email@example.com.
OK, so let's take a look at creating our first Azure function in the Azure portal.
So of course the first thing you'll need to do is to log in to the portal, using your Azure subscription. You can of course create a trial subscription, or if you have one through work you can obviously use that one as well. So, once I'm in the portal, I'll click New. And, just to make it easy, I can of course navigate around, looks like I have Function App at the bottom, but I prefer to type in the search box and it tends to be a little faster overall, so. Type function, get to Function App and click Create.
OK, so now I'm asked for a couple of pieces of information. First I'll need a unique name, this is something that has to be globally unique, since it's a URL that needs to resolve globally via DNS. So I will use something like ca-josh, oh, it won't allow me to use a dash, ca josh func intro, something like that. All right, I'd like to use my subscription. I happen to have an existing resource group in my subscription already that I want to use, you can of course create a new one. If you don't know, a resource group is merely a, kind of a logical container within which you can place one or more resources that you create inside of your Azure subscription. It's kind of just a way to kind of group things together. Resource groups are really easy and convenient, a convenient way to say delete all resources that are kind of contained underneath that resource group all at once, that's one of their main uses. For Hosting Plan I will pick the Consumption Plan. As we discuss in the course, there are two pricing models for Azure functions and all App Service services that run in Azure. I'm going to use the Consumption Plan, which essentially allows me, or bills me on a kind of a just a usage basis, as opposed to reserving a virtual machine for me to use perhaps at all times. And of course that's quite expensive if I'm only using it part of the time, if my functions will only run, say, one or two hours a day, something like that, I don't necessarily want to reserve a virtual machine for 24 continuous hours, you know, across all days in a month, using, that would be the App Service Plan, the traditional App Service Plan. The Consumption Plan is exactly what it sounds like, it just bills you only when your functions are actually running, so that's what I'm going to choose. I'll also pick East US, since that's where the rest of my resources will be running. For Storage Account, you can choose the default if you want to. I'm actually going to just pick one that I've already created, either option is fine, I'm just going to use this one. So then we'll create, we'll go ahead and create our Function App, which will take just a few seconds.
All right, so it looks like our deployment has succeeded and we have our new Function App, so I'm going to click on that here, which should take me there momentarily, ah, there we are. OK, so we have a Function App, but of course we don't have any functions yet. So, in a moment, we'll go ahead and create at least one of those.
Just real briefly, the user interface for the blade for Function Apps, gives you kind of quick and convenient access to a number of umbrella level settings that are configured, or that you can configure for your function application itself. Azure functions sort of exist in this container called a Function Application, or a Function App and you can create one or more functions for any given app. So here's my, over on the left hand side here you can see that my Function App ca-josh-func-intro and that's my application itself, and underneath that I can create functions. I can also create proxies as well, we'll talk about proxies in a subsequent demo. So as far as the basic settings and attributes of this Function App, you can see what subscriptions it's associated with, where it's located, resource group, the URL that you used to kind of browse to, you know, the root URL, the path that you browse to connect to this Function App. You have some additional information, some settings you can set, things like turning on Proxy Support. You can configure Host Keys, again, we'll talk about Host Keys in a subsequent demo, but this is a means by which you can specify essentially kind of API keys that are required to be passed in any request which is going to interact with a function, inside this Function App. We'll use anonymous authentication, or no authentication essentially, just to get started here, but in a subsequent demo I'll show you the use of the API keys feature.
Additional platform settings, because again Function Apps are based on the App Service Foundation, you know, essentially the same infrastructure that Azure Web Apps, for instance, or Logic Apps or API Apps are built upon, then you have all of the settings available to you from the App Service Model or the App Service Foundation, you have all those setting available to you in Function Apps as well. So here we can set things like kind of core application settings.
So if I click on this, you'll see a blade which should look familiar to any of you who maybe have done some work with say Azure Web Apps, where you can set things like .NET framework version, if you're using Java you can specify the version of Java that you're using. You can set application level settings, those are things that you would typically configure in say a web.config file, if you're a .NET programmer. So, again, this should look familiar to you if you've done any work at all with Azure Web Apps, it's literally the same functionality. I won't change anything in there, but just be aware that it's there. Additional things, again, these are all things that if you've done any work with Azure Web Apps you can, it's literally the same capabilities and the same features. Setting support for custom domains, setting things like R back policies, so role based access control policies, configuring push notifications, a bunch of settings on doing diagnostics and monitoring. You can configure CORS support, we'll show, I'll demonstrate some of that actually in a subsequent demo. If you're familiar with cross origin resource sharing, then you can specify CORS policy using this configuration setting. Yeah, those are probably the main things that are interesting in here.
In addition, you also have support for defining, if you're defining a public API, an HTTP based API for your function, then you have the ability to use Swagger, if you're familiar with Swagger documentation for Web APIs essentially, or REST APIs, then you are able to specify some of that information here, which can be very useful for an external consumer, who's going to be consuming your Function App. Again, I won't specify that here, just in the interests of time.
So basically this function will be limited, this is sort of limited in a real world scenario where really the only way you can run this function will be if you click a button in the portal window. Of course in the real world functions are much more interesting if they're triggered based on some event, or if you can call into them via HTTP, but for now just to kind of demonstrate our first function, we'll use one that you just invoke manually. I'll give it the default name, again, just so we can create it real quick and see how it works. So, as you can see, this is a pretty simple looking function. If you're familiar at all, don't worry too much about the syntax if you're not familiar with Node.js. If you are familiar with something like Node, then you'll sort of see that basically we're exporting a function here, which is kind of a standard mechanism that you use in Node, for defining and exporting functions so that they're publicly callable. So we kind of have this window within which we play in Node for defining a function here. And, again, you can do similar things in C#, or some of the other platforms and languages that are supported in Azure functions. We'll use some of those others in some other demos.
But, again, even if you're not familiar with Node here you can more or less squint your eyes a little and imagine what's happening here. Basically, when we call this function, what we're doing is we're just specifying that, hey, we called this function, we're writing something to the log, saying we've called this function with this particular input. And then we just tell the function's run time, hey, we're done, the function's done executing, so that we can return a response back to the caller. So I'm going to save this and then I will run it.
And actually, instead of doing that, what I'm going to do is I'm going to use this test window on the right hand side. So, if you look on the right hand side here, there's a few different windows you can open up. You can see, the first thing you can do is you can see some of the files, we'll talk about this a little bit later, but, some of the files that make up your Function App. And you can navigate these and modify these of course in the browser, or you can upload them if you edit them or create them locally on say your machine, your laptop or something, you can upload them to the portal. There's also a window in here for specifying Function Level Keys.
So I showed you, at the application level earlier, we have a set of keys that you can manipulate and use for defining authenticated access, or basically API Key access to your functions. You can also specify Function Level Keys as well. Again, I'm using anonymous access here, so these keys aren't really relevant at the moment.
So I'll just move away and we'll use this test window here, so we can specify a request body, anything we want. So, “hello from Azure Functions”. So we'll use that and then if I go down here to the lower right hand corner I can click on Run and what we should see, in our log, is, yes, we see some logging output, “hello from Azure Functions”.
So we've, and you can see there's sort of like a running tally here in the log saying that the function started, then we get some output, because we wrote, the function itself actually wrote output to the log and then we see that the function completed. So you can see kind of a basic, the basic monitoring or the basic logging that Azure Functions will do on your behalf. And, again, this works the same regardless of language or platform you're using, whether it's Node.js or .NET or PHP or any of the other supported technologies in Azure Functions.
About the Author
Josh Lane is a Microsoft Azure MVP and Azure Trainer and Researcher at Cloud Academy. He’s spent almost twenty years architecting and building enterprise software for companies around the world, in industries as diverse as financial services, insurance, energy, education, and telecom. He loves the challenges that come with designing, building, and running software at scale. Away from the keyboard you'll find him crashing his mountain bike, drumming quasi-rythmically, spending time outdoors with his wife and daughters, or drinking good beer with good friends.