From the course: Microsoft Azure Developer Associate (AZ-204) Cert Prep by Microsoft Press

Create and configure an Azure Functions app

Let's just talk about here a little bit why functions, why we want to use Azure Functions. And so loads of times we have what? We have code. And we want to run this code based on some triggers. And this is where basically Azure Functions gets a lot of advantages over other solutions because Azure Functions is what we call a serverless solution that allows you to write less code and maintain lasting infrastructure. And of course, with all this, save on costs. Costs on running this, costs on building the infrastructure, all this because you're not going to have them. So instead of you worrying about creating all that infrastructure, deploying, maintaining servers, what you're going to do is, functions will provide you an up-to-date resource that you can just use and keep your applications running. So you focus on the code that matters the most to you, and you can do this in different languages. So as we can see here, we support several languages on this like C#, Java, PHP, Python, even scripting languages like PowerShell. So are you going to run this based on triggers? So this is the trigger. You just have a trigger, and that trigger could be like a timer trigger, but we're going to talk about that later. But let's just say that the timer trigger like every hour and would just run a piece of a small piece of code. So this is great for running background tasks. And there's several scenarios and use cases that we can run on this, but we're going to talk about this, but like this scheduled task, like a data clean up that you have to do like every hour, respond to a database change. Something like this can be easily done by using triggers, because there are several triggers that we have that are built in. So you actually don't need to code this. We usually like to say all the plumbing code around the function. You don't have to write, you just need to focus on writing the code that you need to execute. So all the trigger, all this, it's just built by the platform. We can also have bindings, too. So it's not just the trigger and the data that comes in the trigger, but you can also say, hey, Tiago but I also need more data from that Cosmos DB or that blob storage. Again, you don't need to code to get that data from there, you can use bindings. Just configure the binding. Same for output. In the end, you will produce something and then you need to store that result somewhere. So you might want to, for example, send it to a message queuing using Azure Storage messages or service bus, message queues. Again, you don't need to code to send that message into the queue. You can just use bindings and say, hey, this is the output binding and this is what I want to send to that output binding. So it's really cool what we can actually do with this. I always like when I talk about functions to just give you like some use cases for this. So I have here some use cases like run code based on HTTP request. This is probably the simplest Azure Function that you can have. You're going to get a fully qualified domain name. Yes sure, you can also have custom domains on this. Works very similar, like the app service works. And what we can do is, every time that someone does a GET HTTP call on that fully qualified domain name, your function will run. So the trigger is getting that HTTP request. You can also have schedule code run at predefined time. So like I was saying, just a schedule task kind of scenario. Process new and modified data like you can have like blobs on an Azure Storage. Whenever you get a new blob in a container, boom, your function will run, and it will do something with that. I have done amazing things like this. For example, there's a customer that every time they get a new order, there is a JSON file being uploaded into a blob container. Then we have functions that takes that JSON file that was just uploaded and we process that order. So great way to do it. We can also have messages from queues. You can also have this to respond to Azure Event Grid events, Event Hubs, Service Bus queues, topics. So there's always examples of Azure Functions that we can have. So we can actually create like a reliable messaging system, also in processing those messages using some kind of messaging queuing service. And what about hosting? We mainly have three options when it comes to host this. There is a little bit more, but those are the main ones. Which the first one, it is the consumption plan. So on the consumption plan, you pay for the compute resources only when your functions are running. So it's truly a pay-as-you-go. And this will take care of scaling. So you don't have to worry about scaling this yourself. This is the default hosting plan, by the way. So if you don't change anything, this is the one that you're going to get. We also have an App Service plan, or like we also call this a dedicated plan. So this is the traditional App Service plans. You just check that one out and you say, oh, it's a premium, P1V3, that's the compute capacity that you have. You're going to be charged by the compute capacity that you reserve. So there's this slightly different change here now. Not what you're using because you might only use like 10-20 percent of that. And you also need to worry about scaling this yourself. So there's a lot more work involved in this one versus consumption. Of course, there's also advantages on this dedicated plan is that your functions, and we have techniques to do this, are always warmed up. So whenever you get a call, boom, it gets the response like immediately. In consumption, that might not be the case, okay? You might suffer from some delays when you do the call. And then this needs to warm up the function because now you're in another kind of compute, and that function was never running on that compute. So you might get that slightly delay that we might have. When in the dedicated plan, that's not, that's just not going to happen, okay? We also have another one which is premium. So with premium, you automatically scale based on the demand using pre-warmed workers which run the applications with no delay after the app being idled. It also runs on more powerful instances and you can connect this into a VNet. So sometimes you might want those kinds of features running this in a more private way, et cetera. So premium, yes, it is going to be a great way to do that. So now let's create here a function app. So in order to do this, well, again, Azure Portal, we just select Create a resource, and we use the function app shortcut here. There we go. And we're going to create a function app. Always make sure this is from Microsoft, okay? And you should be good to go. So let's create this. And the first screen that you get here is, hey, what's your hosting option? So like I told you, we have like several of them like consumption, flex. Consumption, that's a brand new thing that's in preview. Premium app service also running this in container apps. So in my case, in this demo, I'm just going with the default one which is consumption. So after this, the traditional part, like what is the subscription where you want this and what is the resource group where you want to host this? And then we're going to be able here, under function app name, just to give it a name. And I'll go with azcontosofunc just to see if this one in here, it is available. Well, let's just wait here a second. It seems that it is still checking. So then we have to choose a runtime. Like I told you, now you can run function apps, .NET, Node, Python, Java, PowerShell, et cetera. I'll go with .NET 8. .NET 8 is great. You can also choose other versions of .NET if you have that need. The region, just again, simple thing, you should choose the region that is closer to your users. And then under operating system, hey, what is the underlying operating system to run your functions? You can go Linux, you can go Windows, .NET 8 runs on both. So you have that option. If you press Next. So next. So whenever you create a function app, there's always a need for you to create a new storage account or to link an existing storage account with your function app. This, it will store some information about the running application, some logging data, things like that. So in my case, hey, let's just create a new one, and I'll go with the default. Of course, you should always change the name here to something that makes more meaning, it's not something that ends in 8865 like we have in here. But for our demo, it is okay. Then we also have some networking options for this under monitoring. We can also set up, for example, application insights for this function or not. This is just a demo, so I'm even going to say no to application insights. And then deployment, you can enable continuous deployment. Similar to what we can also do with the App Service via GitHub Actions. I'm not going to do that. I'm just going to review and create, and just create our function app. Our function app doesn't take that much time to create, especially if it's a consumer, a consumption-based one. So it just takes like a few seconds. And there we go, you should have your function app. And now you can create a project. You can have several functions and host those two, three, one, five functions, whatever number you want running in this function app. So that will be something that we can make it happen in here. Of course, now look, this is creating my function, creating the associated storage account that we need for this. And we should have this in no time. So let's just wait here a second or two to see if this fully created. So there we go. All done. We can now go to our resource, and there you go, we can see our function app just fully created here. If we go functions, we have some more options throughout this just to define everything. But yes, our function app is, yes, fully deployed, fully running. Of course, we don't have any function running on this, but we're going to do that, and we're going to take care of that in the next demo.

Contents