Create Azure Functions with Flask and automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
azure functions it can be really powerful it's based on this technology that is called serverless doesn't mean that there's no server behind the scenes there is one except that you only need to write some code so i'll show you in this video how to write a little bit of code but we'll go a step beyond we're going to use python with flask it takes some special configuration to do that and then we're going to deploy it using vs code the text editor directly to azure to the azure cloud but then we're going to take it even a step further by doing some automation we created some github actions so anytime we're making a change to the code we'll get a new deployment let's see how we can get that all of that done let's see how we can build a a natural function using flask now azure functions are based on the serverless technology all cloud providers have like their own version of what i'm going to be doing here but specifically i'm going to concentrate on azure now the first thing that i'm going to do is going to create a function app from scratch on the portal so i'm going to create a new resource group here and i call it demo function it's a good idea to have some sort of like naming convention so that your resource groups can then be deleted because you can match say for example anything that is demo then you can delete later and forget about not remembering every single service that is associated with the resource group so that's good so demo function so everything that i create is going to be get into this group from the resource group that i just created now the function name needs to be unique so let's do uh something like alfredo demo function and that will probably be available and you get a green check so if you don't so if you do something like demo i'll probably not be available right so let's let's keep it as alfredo demo function and i'm gonna be using python today because i want to use flask runtime is going to ask you about what what type of runtime i'm going to choose python here 39 as a default central us is fine for now and then the operating system is going to stay with linux plan type is consumption serverless and this has like a free a free threshold i don't remember right now what that number is but it's it's enough so that you can you can deploy these test it out and then destroy it and be done with it and not be charged anything all right so let's do the review and create great so i load it and this looks okay and i'm going to go ahead and click create it's going to initialize the deployment and that's going to take a couple of minutes while this is in progress i'm going to switch to my terminal to a new directory where i'm going to be creating the the code so that we can create this function that is now running all right so in my terminal i'm going to use the the code executable you can install this from vs code it's going to open vs code in this case i'm going to say yes here and just so that you can see there's nothing in this directory so i'm going to use code again and dot for saying open right here so this opens my visual studio code editor and i i'm presented with an empty directory there's nothing going on here and i have the azure extension pack installed here if you don't have it you would go here to azure extension pack azure tools see azure functions for sure you would go ahead and install that there we go that jumped if you search for azure and you install this one that's the one you need the azure extension pack installs all kinds of different things to manage azure uh but we're gonna do uh we're gonna use action functions so what these effectively does let me see azure functions it's already installed so that's i'm verifying that that's good to go so i'm going to open i'm going to open the command palette again there's there's no code here i'm going to open the command plate i'm going to say azure functions create create function so actually create new project so this is a new project that's my current directory yes it's going to be python i'm going to use python39 and it's going to be an http trigger that means that whenever there's a request coming into the function it's going to be processed you have all kinds of different triggers but i'm going to use the http one http trigger1 is not what i want to call these i want to call this my main function i'm going to click return and the authorization level is going to be anonymous this doesn't need any authorization level and it's going to start creating so you can see here it's populating lots of different files and that's um that's already getting populated i'm going to make it bigger so we're going to start looking at some of the code all right so it has finished creating the project so that's uh that's good and the next thing that we're going to do is that well we want to use flask so in requirements.txt we're going to make sure that flask is included so i'm going to save flask over here i'm going to save this and that's going to update the requirements and next i am going to go and in the underscore init that pi i am going to you know let's actually take a look at what the documentation says because the documentation will tell you a couple of different things so if you and i'll share these in the links but when you're doing the flash framework in an azure function app it has the flask application in a different file and then it points to this main function here i'm just going to keep everything on the same file because it's simpler to reason about so let's go ahead and do that all right so back to the visual studio code editor i am going to import from flask import flask and then i'm going to say also import request because when uh actually another request let's do the um let's do a redirect i'm going to do a simple very simple redirect function of a function that that is going to perform an http redirect based on on some some input all right so we have that next as you know flask requires you to create an application so when i say flask underscore underscore name which it takes the name of the the the file over here all right i'm going to save that and then instead of these uh oops instead of these code that we have here i'm gonna i'm gonna remove all of these we don't we don't need any of these and then we can leave that login statement there it's always useful we are going to say return func i'm gonna say wsg middleware whiskey middleware app whiskey underscore app and we're actually just following what the docs say here handle request context and then that's fine so uh req is the the the request uh and that's and in the context we need to actually update this thing here which is context and that's the func context there you go so again following the let's try to hide this thing for a second there we go so this is just following the documentation so if you go back to the the web browser you will see that's exactly what we're doing here is effectively copy pasting this function don't uh i'm not doing this one because that's coming from a different module so i'm going to go back here so that looks okay to me and next what we're going to do is well we're going to start creating our flask app so this is this right here would be code for from functions and now this is code for flask for flask application all right so our code for flask application as you know we're going to be routing so app.route i'm going to say anything that goes to the root i'm going to handle this this code is going to be called the index and effectively what i want is to do a redirect we're going to say login that info i'm going to say that how about that we're going to be performing a redirect so um flask up about to do a redirect all right so we are going to be doing a redirect uh how about we do a redirect to um to the portal let's let's do something that takes me directly uh directly to the portal so i'm gonna say and and of course this is very basic but just to get get the point so i'm going to do return redirect i'm going to pass in the the url so that would be https portal.azure.com right i need to use a string here and then i'm going to say code it's a 301 so i'm using something that that is very basic again but just to prove a point right that you can actually use flask i could actually just return an http response i'm going to do a redirect i like doing a redirect all right so next i'm going to save this and write this code right now won't necessarily work correctly and the reason why is because the docs tells us that we need to change a couple of things we need to uh we need to add a a couple of things to the function.json let's take a look at the documentation real quickly so that you can you can see what i'm talking about so here you can see that the function.json is modified to include route in the http trigger so that means that this piece right here needs to be added we can even get rid of the post because we're not going to handle any post requests i'm going to just do a route so let's go ahead and um copy paste these and put it inside our our function.json here in a visual studio code so i'm going to go open the explorer function json is right there and if you remember it's right after this oops and i'm going to put it right there okay so that got updated next what we need to do is update a different file so i'm going to keep scrolling down and the host that json file is updated to include the http route prefix and you can see that the route prefix is now empty so let's let's go ahead and change that so we're gonna need to copy paste these and then go back to our text editor over here and then find this host.json let's see where it is it's right here so i'm gonna click there and um if we take a look at where uh and where that change needs to happen well we need to go and add it right after uh right before the the extensions bundle so i'm going to say i'm going to open that a new a new line over there i'm going to say extensions i'm going to add a comma and you can see that the text editor is already underlining here in squiggly lines that's because i didn't add a comma so expect a comma so be be aware of those you might you might have a typo make sure that you double check that everything looks fine all right so i think this looks uh good to go um let's triple check our documentation here we've updated both json files and then we we can actually install this and run it locally but i wanna instead of doing that i'm going to go ahead and deploy this is brand new brand new application that i just created i'm going to go ahead and deploy it so i'm going to use the command plate i'm going to say azure functions i'm going to deploy deploy to function app is what i'm going to be using this is my subscription and it already picked up that i have an alfredo demo function which is what we created in the beginning i'm going to select that i'm going to get a prompt saying are you sure and yes i am very sure i want to deploy and it's going to deploy check out the output window and yes we definitely want to do that because it will tell you it will tell you what is going on and how how that's uh that's moving along so that's perfect let's wait a second until that completes okay awesome so this uh completed that and now we get these options so let's uh let's look at the stream logs let's let's quickly uh click there and what is happening here is that you you can't really see this because it opened in a different window but i'm going to bring it over onto the view so there you go live metrics not available your app is offline or using an older sdk so uh this in fact is a pretty uh pretty useful i didn't enable the metrics so i'm gonna just close these and um and you can see that this this is looking looking good all right so this is an http trigger it means that we can go to the browser so we can you can see um [Music] let's get the let's get the url let's get the url the url is this guy right here it's alfredo demo function um azure websites so if i go back to my web browser and um i go to my portal and i go to the resource that i created you can see that the url is right there okay oh so it seems that i did have a problem in my extension so i'm gonna have to fix that this is good because i'm getting into trouble and we'll have to figure out how to fix it so i'm gonna open a new tab and this is probably going to function host is now running you can see that there it's i have problems all right so let's go and fix them so it says that host.json has a problem on line 20 okay so no problem let's go back to visual studio code let's open the explorer let's go to host.json so i'm going to close this line 20 is of course right here and i am getting into trouble let's see what it says expected comma or closing closing bracket all right and the reason why we have that is because i open this one this bracket right here and didn't close it so instead um of having this in multiple lines let me um let's uh let's uh make make all of these in a single line so i'm going to do do do these and there you go so i'm going to save these now there's no red i'm going to save these and i'm going to deploy again so the nice thing about using vs code as a nato is that you can you can continuously do this and and deploy the function and select everything again and go and deploy and look at the logs yes i want to look at the logs and it's going to be the plane all right let's take a a quick second there until that completes and and check if if now the the portal reports that this is all correct all right so this has completed i'm gonna close these it says that everything looks okay so let's go back to the browser to the portal i'm going to refresh and now our error is gone i'm gonna go to my function i'm gonna reload and i remember uh this is a redirect so you you you saw that uh i got a re-wreck perhaps that was too fast we can actually try it on the terminal right here so we can do a curl on verbose mode and let's actually go ahead and copy the url so i'm going to copy it to clipboard going back to the terminal i'm going to paste that here and let's see what we get so we get a request the request gets processed is https we get a 301 and then it moves over to the portal that portal.azure.com so that's nice and it means that everything everything is working as we're expected so one of the things that you can do is in overview you can actually look at application insights and get a little bit of metrics that happen within your function here i'm looking at some server requests you can see i got a couple there availability um well it's not capturing availability right now the server request does and the server response time average looks pretty healthy to me so so that's it that's how we deploy it with uh with vs code very quickly but but our application is not it's not right now on uh you know it's it's just living on a directory somewhere so this is not very good if we wanted to if we wanted to actually go and get something um something for for continuous deployment so why don't we go ahead and do that i'm going to create this demo dash function repository it's it's empty i just created this and i am going to go ahead and put all of my code uh in this repository and i'm going to automate it with some github actions and then we'll change the redirect to do something else how about that so i'm going to add a readme uh there we go and then we're going to do we're going to do um [Music] we're going to do i'm going to copy all of these and i'm going to go to my terminal and i'm going to say um well we need to do a couple of uh we need to do a couple of things here we need to do git init and that's going to be for the current directory and uh that's that's fine and now we're gonna say get um at uh everything in here and then we're going to uh commit and push everything and now if we reload that won't have a readme that's fine so to add a function we are going to be using a we're going to be using the the documentation from from microsoft we're going to go to these continuous delivery by using github actions we are going to this allows you to use github actions for deploying an azure function and we need to add the github secrets well that's the first thing i want to do so um we are going to go back to my demo function i'm going to get this published profile that's where you so that comes from orby you get the published profile that's going to download and i'm going to copy copy that to my clipboard and then and then paste it according to documentation as a secret and i need to put it as azure function app publish profile all right so i'm going to go back to my repository settings i'm going to go down to secrets select actions new repository secret and this required me to create an azure uh let me see azure function app uh publish profiles when i copy that go back to the secrets put it there i'm gonna paste that and add the secret now because this is the polish profile and it's supposed to be secret i'm gonna paste it click add secret and then come back all right my azure function app publish profile is there is good to go and next i'm going to create the action now one thing that is useful here is to search for say like something like an azure an azure function so you can say asher and hit return but what we're going to be doing today is we're going to follow the documentation so in here where it says uh create the environment uh you're gonna you're gonna do you're gonna be able to build the function application well let's actually scroll a little bit app and try to find what we need to do in order for python setup an environment is done using language specific so that will do for python building the function application and then deploying the function application that's fine so what we're going to be doing is uh here's the complete here's the complete thing that we need and you can see here that the action function app publish profile is there we need the package path and the function app name so we're going to copy all of these go back here and say set up a workflow yourself and this is going to be let's call this uh instead of main we call it deploy and you know we call it deploy share function and then we're going to [Music] remove all of these and paste our copy from from what we have before oh look at that it already has it already has a name so let's remove that we that's what we got the red squiggly underlines so my app name if you remember is alfredo demo function and you can verify that by going to the portal and that's the name is right here so frail demo function uh azure functional package path is dot that's fine and i believe the python version that we were using is python39 [Music] i think this is not entirely correct like i think this is outdated so remember when we created our our function it said python39 and then nothing here needs to change i'm going to say start commit um actually um let's um yeah let's do let's do workflow workflow dispatch we can say workflow underscore dispatch that allows us to uh try oh that's bad indentation so one of the things that we can do here yes i don't want let me let me let me remove on push there we go workflow dispatch allows us to to do this on the menu instead of on every push you can change that later of course so i'm going to commit that there we go and i'm going to go to actions and then i'm going to see deploy azure function and i'm going to say run the workflow from the branch main and run it and see what happens this will take a second and we'll come back when it's complete all right so our azure deployment function workflow with github actions completed you can see that the rasher functions action was correct everything looks fine now why is this useful because i can come here to my demo function now i don't need vs code i don't need to do anything locally i can submit a pull request and make that update now remember i'm triggering on uh manually but you can trigger on any push but i want to go here to my main code and say for example i want to redirect not to the portal i'm going to edit here directly and i don't want to redirect to the portal but say i want to redirect to azure.com or straight away i'm going to i'm going to commit that update uh that and then because right now right now if we go to my function and i copy these i go to a new tab and put that there it will take me to a portal right so that's good that's my current redirect i'm going to close this go back now i'm redirecting to azure.com so i'm going to go to actions go to deploy azure function run workflow branch main run workflow again this will take another second it took two minutes last time so we'll wait two minutes and then come back but there you go it's now triggered and we'll wait a couple minutes until that's complete all right so that completed it only took a minute so uh that's uh that's good and now we're going to test our uh our little function here so instead of just going through the brow the my regular session because the rear eggs are cached by the browser i'm going to open a a private window and i'm going to paste my my function over here i'm going to run it and see look at that azure.microsoft.com which is it's the same redirect azure.com and then our rerect is working perfect so what we've built today is pretty useful because we've not only created a function with azure using using flask you can see here in the requirements of text we add a flask but we also change a little bit the configuration using host json and then a little bit of code here in underscore in it we created our flask application now i did a very silly very simple straightforward re-wreck with uh with with flask just redirecting to azure before i was just redirecting to portal azure.com now i'm just straightwasher.com and then we also learned how to do that deploying with our code editor here that you can see with vs code we we use the the extensions to do that as well that worked perfectly but with automation going back to here to our repository we created a repository we got all of our code so uh tomorrow if i'm not around and some someone else joins the company and say well how can we update this process well all you need to do is make a change submit a pull request and that will get deployed automatically so there you go that's how you would use azure functions with flask with automation and github actions
Info
Channel: Alfredo Deza
Views: 3,477
Rating: undefined out of 5
Keywords:
Id: ldFJBzSH5cM
Channel Id: undefined
Length: 27min 27sec (1647 seconds)
Published: Tue Sep 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.