Build and deploy a simple Flask application on Google Cloud Run - Part 1 - Deploy a Python service

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone my name is federico tartarini and in this video series i just want to show you how you can develop and deploy a flask application on google cloud run google cloud run is very nice because you can develop and deploy containerized application in few seconds an advantage that you that you have in containerizing application is that each application is going to be one service and then you don't have to worry about dependencies about upgrading the server or anything like that google cloud run is fully managed so it's fully managed by google so you don't have to worry about scaling up if you get more user or scaling down google will take care of all that and especially at the beginning if you don't have a lot of user you're going to be able to use google cloud run for free in the first video i'm going to show you how to deploy and develop a super simple flask application so after you will watch all this video you're going to be able to deploy this application which is a super simple flask application we just display a message and as you can see we get our url so everyone can actually access our symbol flask application in part 2 of this video series we're going to look a little bit more in detail how to develop a full flask application and for part two of our video we're going to deploy this website in part one and part two of our video we are going to deploy our application using the online google cloud shell console so this is very useful because you will not have to install anything on your computer you can just activate the cloud code shell here by clicking this button here and i will show you all the steps and you're going to be able to deploy your application via their cloud shell however in part 3 i'm going to show you how to do that locally from your computer using gcloud in that case we will have to install gcloud on our computer we'll have to authenticate it but then it's going to be much easier to deploy our application so i hope you will find this video useful one thing before we start so if you like this type of video please consider liking the video and subscribing to my channel because it really helps a lot because my channel is still a new channel and if you have any question throughout this video series just leave it under the video and i will try to reply to your question as soon as possible i will also provide the code that i'm using in this video down in the video description i hope you will find this video series interesting and let's start so the first thing that you need to do is to log in into your google cloud platform console and if you have not done it yet you need to create a new project how can you create a new project you can go here at the top so when you start it might look a little bit different so let me go to the main page here let me close the terminal so this is how it can look like at the beginning if you have not created any project you just have to click here at the top and you have to click on create a new project this is going to ask you a couple of information so in this case it's going to ask you for the project name and you can specify whatever you want so for instance you could have example youtube and this is going to be your project id so keep in mind that the project id and the project name could be a little bit different then you have to specify organization and location and then you click on create in my case i don't want to create a new project because i have already a project created here so if you go on the left side of the google console we can see here in the navigation bar google cloud run so we can click on google cloud run and this show shows us our project please note that for one project you may have more than one web application as you can see here i've already an application that has been deployed but i can create a new service which is a new flask web application and these two web applications they can be inside the same project so let's do that let's create a new service we're going to create a new service using the cloud shell so in order to open the cloud shell we just have to click here at the top in the navigation bar this is going to open the cloud shell here at the bottom of the screen and first we are going to create a simple application so let's just check all the files that are inside here the cloud shell as you can see there is only a text file so i'm going to create a first directory where we are going to store our flask application and then we are going to deploy so the first thing that we need to do is we need to make a directory okay and we can do that with mk dear and we have to give it a name so we're going to call it example youtube so after we have created the directory if you use ls we can see that our directory is now here example youtube so now we see the in our directory so now we're inside our directory of course if i use the command ls the directory is empty the first thing that we need to do is to create a very simple flask file a python file which will contain our flask application and we can do that using the command nano and then main dot pi this is going to open a text editor and inside this text editor we're going to copy this very simple code we're going to import os we are going to import flask and then we are going to create our application which is going to have just one root the oma and then inside here we are going to return a simple message which is going to be a low word then we just check that we are calling this file with this command with this if statement and then we run the application we are running it in the bug mode we are using this host and we are checking that this part is available don't worry too much about this plus code because it's a very simple code and i'm sure you ever you already know how to create a flask application if you don't just check on other video tutorials or check on flask documentation which is very nice and it explains very well how to create a flask application we press ctrl save and then ctrl x to exit and now we have this file now we need to create another file and we need to create a docker file we need a docker file because as i mentioned before all our web application are going to be compartmentalized so each will have its own python version its own dependencies and so on so let's create a docker file using the command nano docker file and then we press enter again we open the text editor and we are going to copy and paste this code we are importing python we're going to import the slim version for this simple example then we are going to allow statement to log messages don't worry too much about this you do not have to change this line of code also don't worry too much about this because is we just have to copy the local code to the container image so we are just defining the app home as the working directory and then we're copying the file the local file inside the container this is very important because is the command that pip installed and we're going to install flask and unicorn please note that um google cloud run needs unicorn to work so you will have to have install unicorn even if your flask application locally does not use it okay so that's very important and that's a thing that we are going to have to add to when we are going to deploy a more complex application because we are going to need to install unicorn as a dependency and also the other very important so here you don't have to change anything in this line here you can change the number of threads you can change the number of workers but this is a simple tutorial so i'm not going to cover that what is actually very important here is this last part of the last the last command which is main and app main is the name of the file the python file that we are calling when we want to launch our python application and app is the name of our application inside the main file so let me show you so let me save this file ctrl x let me see like the files that have inside this repository so here i have main and that's why we are calling that main in the docker file but if i open this nano main dot pi this is the name of our flask application which is called app if you change this name you need to change that also in the docker file okay so inside here you will have to change both of these two values if you um your application has a different python file or a different app name now we're almost there we just have to create a docker ignore file and this is just to save some space because we are not going to need all the all the files so we just have to create a docker ignore file and inside here we are just going to copy all the files that we want to ignore again not super important for this tutorial but basically as you can see here you can exclude files that are not really needed for the deployment of your application then ctrl shift ctrl x and now we are going to build and deploy so now that we want to build and deploy let's move to the editor so we can open here in the terminal now we can see that there is example youtube because we created that with our terminal okay so if i click inside here we have docker file inside which is basically just the same file that we created before and main dot pi now let's deploy locally this application so we want to check that everything is working so the pl to run it on the emulator we just have to click here at the bottom here in the bottom bar where we say cloud code and then we click on run on cloud emulator this is going to run the application and we're going to be able to see our application so we are going to change a couple of things so the service name we can call it example youtube so we cannot have the underscore we can just use a dash and then build setting docker you don't have to change anything this is the docker file that we just created inside example youtube docker file everything is fine and we're going to run it as you can see here we have the output of the terminal this is just taking a while i'm going to fast forward this part we need to authorize the the cloud shell so if is you see this message just click on authorize and also you need to make sure that you have a billing account enabled if you don't is going to fail this command and it's going to tell you that you have to activate a billing account you also can see if you don't have a billing account because for instance in this account i don't have a billing account and you can see here at the top that google want us to activate our account and activate the billing account they're going to give you a free 300 credit to start with so you are going to be able to run your application for free for a long time but i just wanted to let you know that you have to activate the bidding account and the only downside of this is that you have to enter a credit card details fantastic we can see that now our application was deployed locally and everything was fine we can close this message if you want to see the web preview of our application we can just click here on this button here at the top and we can click on preview on port 8080 and we can see our application hello world so fantastic now we have our application that is running uh locally uh now we just want to deploy so everyone is going to be able to see our application of course if you're going to make any changes here so hello a word and you can say this is our first application and then you click here on the side on the bug you can just re-run around like our application this is going to launch it again and then we are going to be able to see the updated version so it's going to take a little while great our application is online again and then we can click here on the web preview preview again on the port and as you can see we have changed the text so the last thing that we need to do is to deploy our service and this is also quite straightforward we just have to go back here at the bottom in cloud code here and then we have to click here on deploy to cloud run this is going to ask us a couple of information which we are going to have to change or just verify and then we are going to deploy our application so we are going to select a gcp project okay and we are going to deploy in our project here the one that we have created before so we are going to create a new service so what's the service name well the service name is going to be example youtube and it's going to be fully managed and by cloud run you can select where you want to deploy so for instance i'm going to deploy it here in u.s central but you can select another region which is probably closer to where you live and we're going to allow an entity unauthenticated invocations this is necessary if you want that our application is going to be visible to the public so it's very important that you enable that and then we can keep all the other information as they are and we can click on deploy now if i move this window a little bit more down you will see you know in a few minutes and a few seconds that our application is going to be deployed and is going to appear here inside our project so the deployment was successful and this is our url so now if we visit this url we're going to see our application and this application is visible to everyone because we have allowed that all user can actually see our application so let me just refresh this page here so let me close this actually terminal for a second and then we reload this page here great so this is our example youtube so if you click here we can see all the information the request count request latency and so forth and this is our url that we can share with everyone so everyone can see our application if you want to you can also change the domain name but this is outside the scope of this video via this window here so you can manage custom domains if your application is not visible to the public is because you haven't enabled your information that everyone from outside can actually view this application so it's important that as you can see here under members there is all users okay so that's very important that you see all users here if you don't see all users then your application is not going to be visible to the public and you can only access your application um via invocation which are authenticated so you need to add all users to your application i really hope you found this video useful and interesting and if you did please consider liking this video and subscribing to my channel that really helps a lot especially because i'm a new channel here on youtube and if you have any comments or if you like this video just leave a comment down below under the video and i will try to reply to questions and comments as soon as possible thank you very much for listening and see you next time
Info
Channel: Federico Tartarini
Views: 43,396
Rating: undefined out of 5
Keywords: build and deploy a python web app on cloud run, running flask app in cloud run, deploy flask app to cloud run, cloud run flask, Deploy flask application cloud shell, cloud run python tutorial, Flask google cloud, deploy flask app to google cloud, how to run your flask app in google cloud platform, create flask application and deploy it, build and deploy a python web app, deploy flask app, running python app in docker, google cloud shell, flask python, Google cloud platform
Id: CxzaOHTwqEI
Channel Id: undefined
Length: 17min 52sec (1072 seconds)
Published: Sat Apr 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.