How to create and deploy Azure Function Using VS Code [Python]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi Pedro here from petalist again in this video I'm going to teach you step by step how to set up your environment create an Azure function test deploy in Azure function before I go any further subscribe the channel click the bell and like the video so first set up your environment know you need to open a free Azure account by going to this website here and click Start for free and follow the promise once your account is opened you have to install Azure functions card tool in your system in this case I installed in two so get the version 4. Point whatever is the latest one so for Linux just copy that to your command line so just go copy and paste in your command line here and enter so you do for all these commands so this is for any Linux and then for Ubuntu which is the case of my virtual machine adapt to a cell and then update your Ubuntu system and then finally install Azure function or to use the app get package manager so if you don't know how to set up your WSL Linux so I have a video that I'll put in the description on how to do it once you have Azure functions part tool is installed in your system you now have to install the digital code in my case I install it in Windows so once vs code is installed you have to install some extensions vs codes the first one is python extension so go to here and then python and then the one that you have to install is this one the one from Microsoft after that install Azure function extension you just type Azure function then install this one from Microsoft one thing that you have to make sure is that you have python installed on your system so let me just in the command line again just to check if you have Python and just go python dash dash version so in my case I have a 3.9 115 as a globe installation but don't worry if you don't know how to install python I have a video that I'll put in the description on how to install Pi M and install multiple versions of pythony's system alright you've done all that you are ready to create a function using vs code but before just organize your code you have to follow these steps I'll show you so first step create a directory Where You're Gonna Save Your function codes so now that Linux just go indicator and then the name of the the name of the directory let's call it azure quick function change the directory to the director of the folder you do CD and then the name of the folder you can just type the first two letters and press tab okay inside the folder to open vs code you just write code space Dot and then enter and that's gonna open VSCO in the directory that you just created you can see here Azure functions in vs code we're gonna click the Azure extension that we installed and then on the menu workspace we're gonna click this icon here there is a lightning go to the plus which is create function button once you click that you choose the current directory where the functions are going to be saved after that you click the language which is in this case python then you select the vert of the version that's the python interpreter in this case I have python 3.9.15 and then I'm gonna pick the function type HTTP trigger give it a name they suggest HTTP trigger one and I'm gonna call HP quick and then the authentication mode pick anonymous alright what this will do is it will create a boilerplate for your project we'll have a DOT fan folder with your python virtual environment files a vs code configuration file which tell what are the configurations of the DS code it solves standard and then a folder with the name of your function with two files three files the dunder it needs done the dot Pi which is your function entry point a function.json file with your function configuration you can see authentication Anonymous it type is HTTP trigger I'm not going to get the detail of the rest of the configuration a simple dot that file a functioning in our file which are all the directors and files that the function or ignore I get in our file a host.json file with these with the version of your function a local.settings.json file where you can put a key value pairs if you have any fair to local environment variables that you're going to use in your tests and finally a requirements.txt file before the dependencies of your project in this case only azure functions package all right now that we um notableer plates we're gonna test our function to test is very easy we just press F5 so it's going to open the terminal here install all the dependencies and then launch the function and run locally so there's this HTTP endpoint where you can make a call to this function to do that open your Postman open Postman create a new request go to your terminal copy this URL and this is again request just make a get request with this endpoint that's going to give your error so if I go to the function so I just quickly explain what this function does it's an endpoint that accepts post and get requests and look either for a body with a name key and the value is going to be the name of the person or a query parameter so let's do first a query parameter in the URL you put um question mark and then name and then the equal sign and then the value let's put my name Pedro alright hello Pedro this HTTP trigger function executed successfully the code is working let's try to now give a post and then body raw Jason you're gonna put like a curly braces and then quote on code name Cola quote unquote Pedro let's do uh with a post request yeah the function is working now the function is ready to be deployed so let's stop the function Ctrl C enter so the function is stopped so to the to create a function app you have to connect to your Azure account to connect to your Azure account click Azure and then click sign in to azure it's gonna pop up your browser click the account that you're gonna use so go back to vs code now it's authenticated to create a new resource you click plus choose grid function app in azure enter Global unique name for the new function app in this case let's call Quick function 4232 and then enter the python version and the region in my case Australia East and then it'll pop up the Azure activity log will take a few seconds and all the resources will be created nicer if you expand the page with your description here and go to function app the new function will appear here at the moment there are two functions that I already have in my account just I'm gonna fast forward this in my video so you don't need to wait okay once you see a green tick like that and then we'll see the quick function 4042 that was created now we are ready to deploy so let's go to my Azure account first while signing and then I go to Resource Group I can see that there is a resource group called quick function and then it automatically created a app service plan a application inside another storage account a function app and a workspace for log analytics so this is part of the package to make sure that the function works if I got the function app so there is no function deployed as of now let's deploy our function to this app go back to the Visual Studio code and then clicking on this uh cloud with a error up and then deploy to function app pick the function in my case quick function 4x242 and then are you sure you want to deploy a quick function for each of I2 this will overwrite any previous deployment and cannot be undone click deploy and then they'll pop up here deploy into Quick function for java2 click output window and then you can follow the process will take a few seconds for less than a minute so congratulations your function is in production now copy this link here which is the production endpoint of your function open um Postman again create a new get request click here make a request I have a not successful response so let's pass a query parameter uh name equals Pedro as for the character and then send okay it's work in production let's try a post request with a Json body so curly brace name oops let's put our face just in case and then Pedro let's send voila congratulations you just deploy so let's go to the um my Azure account that's refresh this page you can see there is a function here click Code test so we know that's working I'm just gonna show you that you can test in the portal click test run you can put a getter post let's look at now let's do a post a thousand then you can pause the body so the body again is name and then my name and then I run and I get the response hello Pedro this HP trigger function executed successfully close and before I forget you don't want to paint it's very cheap but you don't want to pay any costs you just click on the resource Group and then click delete this Resource Group and then type the name click function for example two and then delete all right it's gonna go in a few probably minutes or so all right thank you very much for watching I hope you enjoyed my click a quick explanation on how to set up your environment and deploy test locally and then deploy in production see how it is easy so Microsoft make it really easy so if you like this video please click the like button and don't forget to subscribe to the channel and click the Bell so you can receive notifications for more videos uh I'm not sure about power bi about SQL thanks for watching [Music]
Info
Channel: Pytalista
Views: 28,431
Rating: undefined out of 5
Keywords:
Id: HCqe49vero0
Channel Id: undefined
Length: 16min 38sec (998 seconds)
Published: Thu Feb 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.