Creating a Flask Web Server in EC2 on the AWS Free Tier from scratch!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everybody and in this video we're going to be walking through how you can spin up a flask web server in ec2 i'm going to keep this all in the free tier it's going to panny money because i'm cheap and we will get started so very first thing is we're going to be doing also from scratch so i'm at the ec2 instance or in the dashboard right now in the uws management console and the very first thing i'm going to do here is i'm going to click on instances and basically i want to launch some instance and for this we're going to be using a uh we're going to keep this with the old experience here just so that's hopefully this makes more sense uh basically what i want to do is i'm going to only look at the free tier stuff and we're going to be choosing to go with the ubuntu server so i'm going to go with 2204 here and stick with the x86 architecture i'm going to select that and then we're going to also stick with the t2 micro instance type because i'm staying on the free tier and we're going to configure instance details i'm not going to change anything on this page not going to change anything to do with the storage we're going to add some tags here i'll give this thing a nice little tag we'll call this the flask server why not and then we're going to go to the configure security group and this is the part where you got to be really careful because these things matter a lot in terms of how is your security access can be controlled for your web server because obviously you want people to be able to connect to it to serve their web requests so in this case we want to make sure of is that we're allowing traffic uh the very first thing here is we're allowing ssh traffic on port 22 this is just that we can ssh to the machine we also need to be able to send http requests to our box so i'm going to say http traffic and that's going to be specifically port 80 it's going to be allowed to come from anywhere so that's what this inbound security rule means and then we can also add one for https and this is that we can have certificates in the future to make our website a little bit more secure and in addition that i'm going to allow custom tcp traffic and it's going to be on port 8080 because our web server does listen on port 8080 so we're basically saying you know we need to keep all these ports open so that the outside world so any ip address out there can call our web server and if you don't set this up right you will get a bunch of timeouts or page not found stuff like that so basically you see those errors double check your security groups make sure it's right and then to future proof this that in the future if we wanted to have this a part of like an auto scaling group and spin up new instances really quickly to handle more web requests if we had a super popular application and give us a name i'm going to call this the flask web server set group and that just lets us reuse this if we want to down the road so gonna click on review and launch and we're making sure we're using ubuntu we're sticking on that free tier these are security groups policies which is great and i'm going to click on launch right here and in my case i've already created a key pair and i've saved it to my downloads folder but if you have not you could click on create new key pair and put that on here we'll just do it to be really nice so i'll call this vs flask one i'm going to download this key pair so you can see it downloads a new key pair we're going to click on launch instance right now and so this is going to take a few moments and we're going to view on our instances right now and just going to let this guy spin up also going to open up a windows explorer window so that i can put my folder my pen file into it into a place that i can find it and so now that i've done that i'm going to refresh our ec2 instance page still in that pending state so just let's give it a little bit more time okay you can see it's now running so if i click on the instance id it's telling us what this ipv4 address is so what i'm going to do is i'm going to copy this guy and click that button right there i'm just going to paste it in because we're going to remember it for later because that's how we're actually going to connect to our instance so we need to remember this so now we've got that ipv4 address for our ec2 box i'm also now going to open up powershell and i want to now ssh to that and something i'll say that aws does is pretty cool here is if you click on connect click on ssh client and then click on this guy right here it tells you the exact command to run to connect to your instance with that pem file or that key pair file that it's expecting so i copy that command and in my powershell you can also do this in terminal or in the shell and ubuntu i'm going to change directory to where i've saved it so i've put it onto my desktop folder the pen file and i'm going to paste in this command gonna hit enter i'm trying to make this bigger for you guys so you can see it and then i'm going to hit yes and so now what we're doing is we are establishing a connection to our ec2 instance and if we make this guy the full screen we can see that we are now successfully connected to our remote ubuntu server which is great i'm gonna run a little ls thing here just to show that it's completely empty and now what we're gonna do is we're gonna start making some directories in here so that we can do some damage so very first thing that we want to do here is i'm going to run the command sudo apt-get update and so basically what this is doing is just making sure that we've got the latest versions of all the packages before we install them just so that you know you have the most secure web server you can at this at this time we're also going to be spinning up a virtual environment for python so we're going to install python3-v env and i'll paste a link to an article that basically has all these commands so you guys can follow along um but basically you've got all that stuff done and now what we want to do is i'm going to make a directory on my web server so mkdur and we're going to call this thing hello world and that's going to be fantastic just going to confirm we've got that directory i'm sorry you can't really see it that well but we're going to cd into that directory and now that we are inside of this directory we're going to spin up a python virtual environment just so that we can try to be a bit more organized with how we are controlling all the packages and dependencies for our web app so we're spinning this guy up we're going to now activate our virtual environment by running this command right here and you can see now we've got those little brackets right there that tell us we're now running a virtual environment inside of this directory and inside of this directory in this virtual environment we're going to install the flask python package so this is what does the magic of actually handling uh you know letting us do our logic just like that and now what i want to do is i want to make a file inside of this directory inside of our ubuntu server i'm going to call it app.py and it's very important called app.py because that's the default place that flask looks for when it starts spinning up your server so we're going to run the command sudo to have elevated privileges nano to open this up in an editor app.py so it's going to create this app.py file just like that i'm going to copy in this command and you can see that this is just a super super basic flask application obviously we can get some more nuanced um if we want to down the road but i'm just going to leave it as is all it does is say hello world um and so that's all we need right now i'm going to hit control and then o on the keyboard which is going to ask us how we want to save this file i'm going to save it as app.py hit enter on the keyboard hit control and then x to exit nano and then you can confirm that that file is set up correctly by typing in cat and then app.pine it just shows you everything inside that file just like that so basically now we are happy with the way this thing is looking in addition to that what we can do to make sure that flask is running as we expect it to so we run the command python app.py we can see that we've just spun up a web server inside of our server and this is not yet available to the outside world it's available inside of this thing and so we are going to first hit control c and this is going to kill our web server so just give it one second okay so it just ended that little session now what we're going to do is we're going to need to use g unicorn and g unicorn is a wsgi which stands for web server gateway interface and the reason why we need it is because it basically will create a worker to handle each one of the requests that come to your web application so like if user one wanted to see your home page and then user two wanted to see your home page and user three want to see your login page you're gonna need to worker to handle each one of those users sending you those get requests and so that's whole point of g unicorn is to allow itself to happen so uh what we need to do is again inside that virtual environment inside of this directory for our project on our ubuntu server we're going to run the command pip install g unicorn just like this okay and so we've got g unicorn installed and i'm going to run the command like that that zero zero zero means that we're allowing traffic from anywhere and we're also telling it app app tells it to basically look for uh the app.py file that we just called or created for flask and so we can see that g unicorn is now working as expected it's got a little process id because it is a service that's running on our machine and so you know if you're doing this in a production environment i think this is really good practice and stuff to to make note of if you're you know for learning this stuff is how do you make sure that you know let's say someone pulled the power on your ec2 box for your web server and you know you plug the power back in how do you make sure your web server comes back online and starts handling web requests again the answer that is you need to create a service for g unicorn so i'm going to hit control c again on my keyboard and this is going to basically tell g unicorn to stop and what we want to do here is we want to move forward and basically create a service for g unicorn so that in ubuntu as soon as ubuntu as soon as that operating system comes online it will say i need to start up the g unicorn service to handle that flask application that we just created so we're going to do that right now just going to give this guy a few more control c's to make it shut off and so now we're going to do is we've got unicorn installed and i'm going to run this command as well so going to paste that guy in there so we're basically editing a file under the etc directory and it's going to be creating this little service so it's brand new file and what we're going to do is just paste in this code right here i'll provide links to all this stuff so you guys can follow along what it does but as you can see create a service and um restart always means that you know once this thing boots up start that unicorn service that we don't have to worry about it so now we are looking good on this so i'm just gonna hit ctrl o again on my keyboard and we're gonna save this as the file name that we initially proposed which is great and so now that we've done that we need to actually enable that service that we just defined for to unicorn uh with these appropriate uh settings so i'm gonna hit ctrl x to exit now and then what we're gonna do is we're going to run this command or the series of commands actually start a service so it's going to reload all those services that exist on our machine because we just created a new service specifically it's called helloworld.service we're going to start the service and then we're going to enable the service all right so now we've got our service enabled and created and now basically what's happening in the background on this machine is we've got g unicorn which is that wsgi that we talked about and it is basically rendering that flask application that we defined uh that hello world thing so if i run this command curl local host port 8000 just like this you can see that our little flask server just threw back hello world because that's what we told it to do and we could have it start throwing back some html as well and do some fancy stuff but for right now this is all we needed to do so we've got the web server able to call itself from itself but we want to make sure that other people outside of our application or outside of our you know domain or machine can call our web server because we want to show this to the rest of the world so basically uh what we're going to do next here is we're going to perform the following so we're going to install nginx and nginx is going to basically handle routing requests that come to that web server port 8080 to the appropriate place so we basically want to say nginx talk to g unicorn and unicorn talk to your flask application um so i know it's a little bit tricky right now but basically bear with me um so basically uh we are good because we are getting that nice response from that curl command so basically just pinged it or sent a get request to that um now what we're going to do is going to run the command sudo apt-get install nginx one second sudo apt-get install engine x just like that and i want to continue and just give this thing a moment alrighty so we've got nginx service or the package specifically installed now we want to start and enable it so again i'll provide all these commands in the description so you all can follow but we're basically starting it enabling it and we are now now that we've just started nginx uh if we open up a new browser and this is the really cool part you can open up this browser just like that and if we remember that ip address of our that public ipv4 address that we created earlier i'm going to copy that i'm going to paste this guy right into there and now when people are going to our web server they're hitting that http or they're hitting that ipv4 address they are getting that nginx welcome message but we want them to see the flask hello world message they want you know we want to render our actual flask web application so what we need to do next here is we need to uh modify the default values for nginx to point to g unicorn to point to flask and so to do that we're going to run this command so i'm going to paste this guy in just like that so basically modifying another file right here and i'm going to just do some copy pasta so we're going to hit the down arrow create some a few lines here paste in this specific thing so basically uh we're telling uh nginx to make note of this new variable called flask hello world and the other thing we're going to want to do here i'm going to scroll down a bit is we're going to modify the actual proxy for the location and so this new location is going to be the following so again bear with me it's basically telling it you know every time it tries to render or every time nginx sees a request this is the first thing it should do so call that proxy pass and we're gonna do that flask hello world uh variable that we've defined at the top of this file right there so now we've modified this thing i'm gonna hit ctrl o again to save this so hit enter to save it then we hit control x to exit nano so now we've successfully modified the nginx default and we're going to restart the nginx service because we've just made changes so now we've just restarted nginx we are now back in business so basically uh that we've made these changes and we restarted the service if someone again goes to a new window and we paste in this public ipv4 address just like that no longer are we seeing the nginx uh page where we are being redirected through g unicorn to our flask application so just like that we have successfully spun up a flask web application for free on ec2 so i hope this stuff is useful for you guys you can start playing around with this and you know get more fancy upload some pictures and other web pages and templates and routes but you know for the purposes of just getting started with something that works this is hopefully helpful for you guys so thank you all for watching let me have any questions and i'll talk to you guys next time
Info
Channel: Vincent Stevenson
Views: 28,199
Rating: undefined out of 5
Keywords:
Id: z5XiVh6v4uI
Channel Id: undefined
Length: 17min 28sec (1048 seconds)
Published: Tue May 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.