Setup Your First Azure Functions App (Python) using Visual Studio Code | Step-by-Step Guide 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello everyone. Today we will see how we can set up an Azure Function app for Python using the Visual Studio Code. One thing you must have installed before proceeding with the video is Azure Function Core Tools. It is a command line tool. Let's go to the page. I will give this link in the description below. On this page, scroll down a bit. Make sure that version 4 is selected. After a bit more scrolling, you will see that it is available for Windows, Mac and Linux. I will download the Windows version. Click on this and it will start downloading. Now I have it already installed on my system, so I will not install it again. But you can install it like any other software by double clicking on this. Now let's go back. Let's go to the VS Code to start the process. So in the VS Code, the first thing you need to do is to install the relevant extensions. Search for the extension Azure Functions here. In total, we need to install three extensions and don't worry, I'll give you the relevant links of all the extensions into the video description. So first we will install Azure Functions and Azure Account and Azure Resources will be automatically installed. So these are the three extensions that we need. Once these three extensions are installed, you will see this Azure icon here. Click on this Azure icon. Now as you can see here, there are two sections in this extension panel. First is Resources and second is Workspace and it is asking for us to Sign In to Azure. Here you need to Sign In with that Azure account on which you want the function app to be deployed. I will click on sign in to Azure and I will select the account with which I want to Sign In to the Azure. It will show this you are signed in and now when I go back to my VS code, I will see the screen here. This is the subscription, free trial subscription. This is which I'm using and if you have any more subscriptions, you can click on this filter icon and I'm seeing only one subscription, but if you have more than one subscription, you will see all of them here. I will select this and click on OK. The next step after Azure sign in is to create a function on your local system first. For that, you need to click on this icon, create function. I will click on this. I will go to This PC, Local Disk D and I will select my folder. I created a folder before. Yeah, Function Testing and I will select this. Now it is asking me which language I need to create the function in. So we are using Python. Here you will also see Python programming model V2. So this is the new structure of Python model which Azure has recently introduced last month. If you want me to create a video on this, you can let me know in the comments and I will create a video for that. For this video we will stick with Python. I have selected this and here it is asking you if you want to create a virtual environment or not. I do not prefer to create a virtual environment, so I will just skip creating this virtual environment. And then it is asking me for the template. Now this template, it means what will be the trigger of your function. So HTTP trigger is your function will be triggered with a URL. Timer trigger means it is a cron job basically and your function will be triggered each day or each week or each month on a specific time. So for this test purpose, we will use HTTP trigger. Now it is asking me to provide a function name. I like to follow a naming mechanism or naming methodology for the functions. So I always append func- in front of my functions. Here after that you can name it anything. Let's say we are calling it testing func. Now authorization level, let's keep it at the function level only. You can keep it anonymous or the admin. Admin will give it all the access. We'll keep it at the function level. And now it is asking me, this is a VS code. What it is asking me, it is asking whether I need to create the local folder in the current window or we need to create the local folder that we just created in a new window. We'll just open it in the current window. And in the same VS code, you will see that in your object browser, it has created it. I trust this and all the folders are now created. Folders and the relevant files for your function. You can see them..VS code func-testingfunc, which we just created, which we named our function, init.py, function.json and everything. Now VS code is telling me that I have not installed Python extension. So let me install that. Now VS code automatically detected that we have created some Python files in our current workspace. So it suggested me that. Now in the background, when it is happening, let me give you a brief overview of it. Here you can see this is the host.json, excluded types, extension bundle. All these are Azure function properties and you can manipulate your function if you change the values of these properties, I will link down in the description, the documentation of Azure function app using which you will be able to see what each property means here. Or if you want me to create a video on that, let me know. Now, once the process of creating the function on local is completed, the next step is to deploy this function on your Azure Portal or so to say on your Azure. Now for that, you need to have an application or a Function App created on your Azure. And for that, we need to go to this section, resources, right click on Function App and create function. Now you will get two options, create function app in Azure, create function app in Azure advanced. So advanced just gives you some more options to play around with. So I will choose advanced so that I can show you everything. Here you need to enter a globally unique app name. Globally unique means this app, the same name should not be used by anyone anywhere else in the world. So we will name it something very unique, app-test-mayank. Okay. It is a unique name. Let's enter it. Please do not follow the naming practices, which I just followed. I'm just using them for this testing purpose. We will use Python 3.9 because 3.10 is still in preview. It is asking me to select a Resource Group in my Azure. I do not have a Resource Group right now, so I will create a new Resource Group and I will call it rg-testingazure. It will ask me for the location. We'll just select East US. So there are three types of function apps, Consumption, Premium and App Service Plan. We will stick with the Consumption. It is the cheapest one. Premium and App Service Plan have their own costs. Now it is asking me to select a Storage Account. I do not have a Storage Account, so it is asking me to create one. I will use st-testingazure. It is asking me to create an Application Insights Resource. So application insights basically helps you to go through the logs of your function. You can skip or you can create. Let me show you how to create. Just click on this, create new, and I usually prefer to create the resource with the same name of app, application insights and function app, same name. I usually like to keep it that way so that when I go back, I can match them up. So I'll just keep it up. Now it will take a few minutes, this process. I will forward this when I upload the video. Now as you can see here, successfully created the function app. Our function app is successfully created and you can verify that if you click on this icon. Okay, for some reason it is not showing it right now. It may take some time to load here. Now we come back here and we can see that now our function app is visible. Sometimes what happens is it takes some time to show the app created in this resources window. What I usually do in that scenario is I press ctrl+shift+p to access the Visual Code, Visual Studio Code Command Palette and I run this command python clear cache and reload window. It will reload your VS Code and usually then the application that I create starts to appear. Now the next step is to deploy the local project or the local function that is this function which we created to our application that we created on Azure. For that, we need to click on this icon deploy, deploy to function app, select your application name, app-test-mayank, the same name as here. Click on this, it will show a pop up in two-three seconds. Okay, this is the pop up. Click on deploy and then it would take around one minute depending on your function size or the data in your function to deploy. I will fast forward this in the once I deploy the video or upload the video on YouTube. Alright, our deployment is complete and you will see this pop up deployment to app-test-mayank completed. Now how can you verify that your deployment is completed? For that, you need to go to Azure, in this global search bar search for function app. I can already see function app, but if you don't see it here, just type function app and select function app from here. You will see the app name. Click on this. Click on functions here. And here you should see the function which you created on your local func-testingfunc. This is the same function that we have created on our local. Let me quickly show you. Local disk D function testing this func hyphen testing func. func-testingfunc. Now it means it is deployed. If you want to see the code, you can click on the function. Come to this Code+Test. And here you will be able to see all your files. It may take some time to load. Alright, it is loaded. You see this init.py file. The same file you will see in your func-testing init.py. This is the file. And you will see all these files function.json sample.dat. All these files will see a function.json sample.dat. So this is how you can verify that your deployment is completed. Now it is time to run our function. Now once the deployment is complete, you can further expand this app, expand functions and you will see your function here. func-testingfunc. Right click on this and you can copy your function url. Copy this. Go to your browser. Enter the url here. Press enter. And you will see this line. This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response. And if you come back to your function in your VS Code, you will see the same line here. This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response. Now if you pass the query param with name, you will get this response. To verify this what you can do is come here. Now the query params have already started from here. So we don't need to type a question mark. We just need to type & type name=Mayank name=Mayank and the response that we got is changed. Hello Mayank. This HTTP triggered function executed successfully. So this is how you can run your HTTP triggered functions. Now let's try to make a very simple change in our function and see how you can deploy the changes. Let's say I have two numbers. a is equals to 2. b is equals to 3. And c is equals to a + b. And I want to see the value of c in my responses. So let's come here. c. And let's come here. Add f in front of it. The sum of a + b is in curly brackets, I will type c. I will save it. And as earlier, any change you made, you need to deploy that change. Click on deploy. Deploy to function app. app-test-mayank. Get the pop-up. Click on deploy. Now this time I will just skip this part from the video. All right. The deployment is complete. Let's test it. Again copy the function URL. Go here. Open a new tab and do not pass any name. Press enter and you will see the sum of a + b is 5. So in this way you can do pretty much anything. I just showed you a very simple change but you can do anything in here which you can do in a normal Python function. So this is the complete process of deploying an Azure function for Python. Let me know if you have any doubts in the comments below and I will try to answer all your questions. Thank you.
Info
Channel: CodingInfo
Views: 45,790
Rating: undefined out of 5
Keywords: azure, python, azurefunctions, vscode, visualstudiocode, python39, azurefunctionapp, tutorial, guide, howto
Id: YQtVJ7qVXDs
Channel Id: undefined
Length: 13min 13sec (793 seconds)
Published: Sun Feb 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.