Azure Function: Creating and Deploying HTTP Triggers and Timers to Azure using Visual Studio | LSC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel so in this video I'm going to cover a few topics about the Azure functions and which is very important with respect to the Azure certification az204 which is nothing but the developing solution for Microsoft Azure okay as dot net developer and if you are focusing on Azure Cloud this is the important uh certification that you need to finish and it covers a lot a lot of development activities okay and varieties of resources and different functionalities over the cloud all right so let's focus on this if you go to this learnsmartcoding.com I have I have a post which talks about uh this certification and the certification is as per the Microsoft this is going to be as of April 28 which is uh tomorrow I was recording this on 27th and this is what is covered in the examinations okay so you can see you know each topic has different weightages and each one has subcategories and a lot of things right so what we are going to cover in this video is implementing Azure functions and then the first one creating configuring and Azure function and I will show you both visual studio and as well as data Shield portal with different real-time meaningful Azure functions and how you can easily learn this all right come let's dive in before we dive in to the real demo right let's let's understand what is azure functions and why they are useful why you need to learn it right so Azure function is a cloud computing service that is offered by the Microsoft right so that allows develop us to build and to run a small piece of code and they are called as functions okay and these functions responds to events or any triggers so to think of a function as a small independent unit of code that are designed to perform specific tasks like processing your data uploading a pro in image or processing an image running a script or even you know responding to an event so these functions are triggered by various events such as HTTP request change in a database or even a timer based control Azure functions are very useful because they provide developers with a way to quickly and to easily build and deploy an application without a developer needs to worry about and managing any infrastructure so think of this if you want to deploy an API what you have to do you have to build an API using a.net core project and then how do you deploy that you need to have an app service or you should have your own you know IIs configured so you can deploy this website all those infrastructure right so you don't need this within two minutes you can you know complete an API deploy to this Azure function and that's it it will become a restful API endpoint and you can use that endpoint based on how you how you use like based on the consumption you don't use it you don't pay it with the Azure functions developer can focus on writing the code that performs a specific task rather than worrying about all these infrastructure stuff right and then in addition there are few more important things right Azure functions are cost effective they are cost effective because like I said they build only when they are run the developers only pay for the resource they use if your API is very simple API which doesn't consume too much of uh power literally you'll pay nothing to Microsoft okay that is what it means so Azure functions are also highly scalable meaning let's say all of a sudden you have a lot of customers and a lot of traffic coming up for your API you know Microsoft will take care of scaling these uh apis automatically you don't need to worry about any infrastructure you don't need to worry about any downtime all are taken care so beautiful right so overall what is azure functions they are very powerful tool for building even driven applications microservices particularly they are useful for developers who want to focus on writing the code rather than the manage the infrastructure now now you have some Concepts uh understanding of why what essential functions why do they have uh why do they provide such a beautiful things and you know how easy and how useful it is right so you will eventually Implement uh these things in your organization for your project uh and if you do so already let me know in the comment section and come let's dive in so I'm using visual studio 2022 you can use 19 as well 17 also will support but we are going to use dotnet core 6 okay so just create a new project select the function app and give a project name give a location where you need to save the project don't worry about all these codes focus on the video I will give you every single piece of code with the solution in uh in the description you can take it from the my GitHub repository okay so let's focus on this the framework that we are going to use is the dotnet 6 and you can see as soon as I select this there are all the possible uh you know Azure functions which does different works are listed here so let's let's see HTTP trigger and you know we will we will talk about various things but let's focus on HTTP trigger okay which means someone is going to call this endpoint that is what an HTTP trigger means okay the three levels of authorization function Anonymous and admin function is basically you need to pass a authorization code which is called key only then it will work Anonymous means it's open API you can you are exposing this to public and anyone who has to revolve can you work it okay so based on your need you can do it now let's focus on this as soon as I selected this there's a pre-built code which is already in front of my uh you know ice right so I will go over this a few files and I'll explain you how it works the first one is the local setting Json and this is the host file local settings basically will have the configuration for your connection string and all the other stuff which you have to use and it's a simple launch settings and service dependency okay let's focus on the function which created by the template okay it's it's not something that I created right so if you look at this um the project I'm going to rename it but if you look at this only one SDK functions is installed as and when we start doing many things we will install different packages so if you come to this function I rename this function uh to a simple demo function and then this is the function name so basically what this function is doing is it is going to have an attribute called HTTP trigger and then the authorization level is function and then we're gonna run this okay so basically when you run this you can see it will give you a option called git or a post you can invoke this API URL which is running in local once you deploy I will show you how the URL will look but if you run this either through get or through a post it's gonna work but if you look at this this API was having a name property and let's debug this right so a get or a post will invoke this endpoint we are doing a logging and then we are trying to fetch a name from the query parameter if it is there it is going to read the data and display it it displayed it okay now if I change this to a different name meaning the parameter which is a name so I'm giving something else right so it will eventually go and look for the body and if it isn't fine it will throw you a 400 you know error stating that you need to pass it that's what the simple code is doing and you can notice that HTTP trigger is the attribute which will uh determine the attribute will determine what kind of function is this so we are going to create our own function right click on the project click on ADD add new Azure function so this function is going to be a new HTTP timer function basically HTTP timer means with a given interval of time this API gets invoked automatically that's called HTTP timer function okay so timer trigger they call it tax so what you do uh you you select something called timer trigger and this schedule will come this is called con operation 0 or in con expression okay so the expression is explained in my blog post basically we are saying every 10 minutes invoke this that's why the second parameter was minutes okay so star slash 10 means every 10 minutes it will invoke this function now you can see the attribute is timer trigger the earlier one was hashtipital girl so that's I said this is what it determines okay now what we are going to do is we are going to add a simple HTTP call to this endpoint okay I'm going to replace this with a piece of code what this is doing is it is calling this essential product API endpoint so that every 10 minutes this is called my uh my apps web app which is deployed will never go to sleep because if it is idle for 20 minutes the application will go to sleep and then the next request comes it takes time I don't want this to happen so this is a simple uh real-time example right so you keep this app awake so now you can see uh I can I can see the second one is the timer trigger that's why you don't get the URL because you're not invoking by yourself it gets invoked automatically okay so let's go and publish this and see how it works okay so in order to publish you select uh the population and then start selecting Azure and then select the uh Windows app or it is a Linux container or whatever it is before that if you already have a portal use that login and I already have it you see this there's one application which is there now I wanted to show you how this came so that you can you can deploy this from your uh from a visual studio code I mean you can deploy this from your official student okay so what we are going to do is we are going to switch back to portal and see how this works how we have to first set up this Azure functions and then I'll show you how this deployment works log into your portal search something called function app once you select the function app it will come here and because I already have something it is listed here but you click on create it will take you through this page this page is similar to the other resource that you create so start filling up all the information starts with a subscription a resource Group I'm going to keep uh the name similar so it'll be easy for me to identify so the name that you give should be unique and if you look at this closely it ends with DOT Azure website.net basically when we create and Azure function app it is equivalent of creating an app service okay within this app service is what our functions are deployed but we don't pay we pay only for what we use and if we look at this the runtime stack you selected and then the the last one the hosting option right it's preferable to go to consume consumption which is cheaper so I select the first one and then storage for a function app to process to work right storage is a must storage account is a must so if you don't do it it will force you to create I selected a storage account of my own name and then networking leave it as it is because it needs to be a public endpoint uh based on your requirement it will change but going to the Monitor and then deployment for now we are not going to deploy anything from the GitHub so I disable these things let's have a tag tag is to identify uh you know what resource goes for what okay it'll be useful uh thing so I'm gonna give this for the demo for Azure az204 everything looks good click on create deployment started it takes few minutes to deployment to finish and while we speak all these things are coming up deployment is done click on go to Resource and if you look at this resource this is the one ends with vs okay visual studio just to have it uh now come on refresh whatever I created there will come here it's loading it's picking up from the portal let's give a minute okay refreshed and I can see the second one great now remember this anything that you use from Visual Studio okay it's read only when you deploy there same thing whatever you do it directly on the portal which I will show you shortly you cannot edit that in Visual Studio okay both are independent both are different what you do here is here what you do there is it's gonna be there okay there's a live editor which we will see for the portal deployment I mean when you do it in Azure portal directly anyway now these two are the endpoints that we have one is the HTTP trigger one is the timer trigger I choose all this publish options and when I publish app is ready let's go to something called functions this is overview right so if we go to functions you see these two apps are deployed one essay is HTTP the other one says timer great now if you go inside any of these okay uh you can see to your right side Code test integration Monitor and function keys basically Code test is something that you can test your endpoint but it clearly says you cannot do any of these things you have to do some configuration because like I said you you are developing something in visual studio and when you deploy only the dlls or deploy it doesn't deploy the code assets then it doesn't get synced with your stuff and the one which is in the portal that's why you know uh it is always like this now you can see the bindings it can tell you clearly from where this uh see this is the you can see the script file it says az204 function demo.dll okay basically whatever we deployed is the it's a deal okay so it runs from the dll so the code always sits in your solution not here okay now you can choose a get or a post based on the method it automatically pops up that and you can either pass the query parameter from here as a as a query parameter and if you say let's say name and then I pass a name here learn smart coding if I do that if I run it uh you can see the response hello learn smart coding this was triggered successfully and there is another option that you can choose the same name and pass it into the body use the post use the body there is no body in the get request so remember it so pass the same value here let's let's say I pass something different so we know what it is right so hi subscribers let me see this one should come when you run this let's see what happens it doesn't come right that's because the piece of code that we have will look for something from the query parameter first because it do exist it doesn't check that from the body so let's delete this let's run this now it will look for the body you see this has changed hello hi subscribers this is a HTTP call great now you know how to test a piece of function after deployment also uh you know how to test it you can run it from local you can also test it from the from the portal now like I said right it looks for the for the query parameter if it doesn't come then it'll look for the body and then if both are not present it's going to throw you a 400 status code but it's not it will process the information and it will give you a 200 okay object resources nothing but HTTP 200 status code okay great now similarly this integration monitor is basically uh you know to show you the logs what is happening on your code and all those things okay now if you try to create here it will give you this kind of an option it basically says your app is kind of uh with respect to the visual studio code not the portal that's why you don't see these two options okay only these two editor option comes it doesn't comes with the direct creation I'm going to switch to a different uh app and I'll show you how it looks okay so this one is uh the function app which has created in Portal not from and nothing is deployed from the code so when I do so if you go to functions under the functions uh slide right you can see a lot of functions listed and app key is basically the keys that is used uh to trigger this function uh whenever you choose a function at a at a function level I mean the access is the function level these are the keys which are used okay now let's focus on something Basics okay so here if you click on create what will happen is you will be uh given an option to choose what kind of trigger which then come uh in the earlier one right that's because you see this development environment there's something selected as development portal that option was not coming that's because of you deploy that uh in the code that's why now here I'm I can choose HTTP trigger and if I choose this I just need to name the function here and then same thing authorization level you can choose what is the authorization level okay all right um so let's go click on Create and let's quickly uh do something useful here so sometime backs I was referring to this development environment right see right now you see three options but earlier you didn't see that option all right so let's select that environment as development portal and you can select uh one of those available in HTTP triggers and we are going to choose the timer trigger name something meaningful I'm going to name it as a wake up essential uh product API like I said I have an API which I wanted to call every 10 minutes or 15 minutes so that it's kept alive just given you know realistic example right and and as soon as you choose this this is a schedule okay I will explain you this on expression in my blog post you can go and read it basically it's a time uh how was milliseconds you know all those combination days years and all those things so right now every one minute this is gonna call okay so once you select this all what I'm going to do is I'm going to Simply copy paste whatever code we have it uh which we which is already working so I'm gonna copy paste the same code and here once you go to the timer trigger and go click on the code plus test and you see this and there was an editor okay so once you have this editor you can place a piece of code and right now I placed uh whatever the code was there present here okay so whatever code is there I copied and pasted here all the codes are here so let's save this and uh I'm gonna show you something instantly that what happens if you do some mistakes okay uh so like right now there is a mistake it's okay I'm going to just run it and show you how what kind of error that comes okay so all good so let's replace um uh the proper function and then let's say let's save it and run it okay so this is the timer trigger so which we already configured so you don't need to put it here I removed that and then uh given meaningful name uh my name also was given when you created it the name is actually timer trigger one so you don't need to put a function name over there and I'm going to uh save and going to run it saved it use the master key on any key for now this is just for demo purpose click on run it's running and let's say there's something wrong here right it's gonna give you uh that it's accepted but you can see the logs that something failed you can go either to Monitor and you can see the number of invocation that happened and then you can also check the logs locks will tell you what went wrong if you have a try catch and if you wanted to display some information it'll be useful you can click on locks like basically connects to the to the integrated application insights and it will show you the logs as and when the when the trigger is running as when the function is running all right so let's go back to the code test uh you know let's let's run this and show you the logs over there itself Okay click on the code plus test and I'm going to run this and you can see this lock will show you here and it's clearly says it failed okay there's something wrong so all what I'm going to do is I'm going to copy completely paste it and we basically don't need the all these things so I'm going to remove this everything looks good save run and it is going to still show you some error I I really want to show you what happens if you get an error right how to solve it it will basically tell you what kind of Errors it clearly says there is no namespace unlocked when I copy paste it I copy pasted the namespace also so let's cut this remove the namespace we don't need it it's basically a class and then that's a static class so once I do this see I started getting the values automatically all right so if you run this again it's gonna you know execute and pull up the information based on the categories and it is just dumping on the log okay so this is the overview so what you need to understand here is you can see the locks here whatever is running it will show up in this chart it'll show you how much errors how much the count it happened how much memory it utilized all those things and you can see there's something called get function URL get function URL will come only when it is a URL based triggers so these are timer triggers right so let's say we create a function which is an HTTP trigger for an HTTP trigger definitely uh the trigger is basically you call you are going to call the API that's called HTTP particular right so when you choose an API which has which is possible to have a URL that will give you a enable the get function URL okay so let's name something his name must get category categories okay so I'm gonna create uh it's a simple function okay started creating and then I went to this code plus test okay so basically this is the HTTP trigger okay so if I uh if I copy paste the working code here what will happen uh it just needs a HTTP endpoint and here what I'm trying to say is if you look at this get function URL it will ask you what kind of key that you need to choose and you can select this and basically you can put this here and if you closely monitor the first part of the URL is nothing but the the Azure app servers that we created right and let's see you see this the one that I'm highlighting that's the one that we created so this URL will have uh the API slash the function name question mark followed by you need to pass the valid key right now it is giving us exception because there is a fault in this piece of code see this is the difference between uh developing a functional in Visual Studio where else you will find most of the errors up front and whereas you do something in the portal directly if you know a piece of code which is easy and you know working code where you don't need to debug so much you can put it in a ship portal if not uh preferably going through the visual studio will be helpful all right so what I'm going to do is I'm going to run this again and see there is definitely an exception it clearly shows you that HTTP client which we used in line number 15 do not have anything anything like that because we forgot to put this and this uh once you put the static variable you know most likely this should work and let's wait for the result okay so basically once you run this you will get a response here so right now the URL code everything is good so I'm gonna run the same thing here and I got the result and it was just 43 milliseconds okay so our consumption plan is up and running and I will give you the link of uh the Microsoft Azure where you can find what is the cost it is really really cheap first 1 million request is free literally free one million is like 10 lakhs that you call it in India so these many calls are free which means think of this when you learn you start developing you start using it you know you will be never charged unless you do a very intense work on your API call okay which will consume a lot of memory and all the stuff but in simple words literally it's gonna be free for developers like us okay all right so you can also see the function keys you can generate the function keys you can rotate the function keys all these Keys comes from the section so as in when you go through inside each of the function you will explore all these information to your to your left side thanks for watching if you like my video don't forget to subscribe my channel like it share it comment it and never forget to click on the Bell icon
Info
Channel: Learn Smart Coding
Views: 3,019
Rating: undefined out of 5
Keywords: Azure Function, HTTP triggers, timers, Visual Studio, Azure cloud, development environment, deployment
Id: zBj4sOa2PvU
Channel Id: undefined
Length: 25min 40sec (1540 seconds)
Published: Mon May 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.