Python + Flask Deploy to Amazon Web Services Elastic Beanstalk (CI/CD with Git)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome it's caleb in this video i'm going to be showing you how to create a python web application and deploy this to amazon web services so the goal here is to get a web page that anybody can go view and we're going to start from scratch now we're going to be using flask to do the python web development however it's going to be fairly similar with django and it's also going to be fairly similar if you're on windows you might just need to change a few things here and there now this is in general a more challenging part of web development and what tends to happen is people will build out this complex application locally and then they'll try to deploy it to some server something breaks and they're not really sure what happened so my approach is we want to isolate the smallest piece of this which is taking the most simple application and getting that working online and then we can add components as we go once you understand the principles you might be able to just take those and bring those over to your other applications to deploy so the very first thing is i want you to open a terminal i'm in sublime so i'm using this terminus plugin and this allows me to have a terminal right here in sublime and i am in this same directory as what we're working in and one of the things i want to do here is create a virtual environment which is basically another step you have to do but it's going to isolate your web application and all of the dependencies for your web application are going to be just for that specific application so that means you can have multiple applications with different dependencies and everything can still work so to show you this we're going to take a look at pip and we're going to hit list and you're going to get a bunch of different python packages that we can use in our python projects however different projects are going to have different versions of these so we want to basically isolate a smaller section to have their own dependencies so the way you can do this is you can say python and then you can say hyphen m v e n v and then you're going to name a directory and i just like to call it dot v e n v so that's where all of our virtual environment junk is going to go in this hidden folder called.venv so from here you can activate your virtual environment and to do that you say source dot v e nv and then we're going to go in the bin directory and then we're going to go with activate so this is where a difference on windows is going to show up there will likely be a shell script and you can look up how to launch a virtual environment in windows if you need a little extra help anyways i'm going to activate this and you're going to see this dot v env on the left which says we are in our virtual environment and now when we say pip 3 list you can see there's nothing there so it's saying i should upgrade so i'm going to say pip install and i don't actually know if this matters here but i'm putting pip 3 just to make sure i got everything working right all right there we go so we got the latest version of pip now we can install any dependencies that are specific for our project so to do this we're going to say pip 3 install and the most important one here is going to be flask all right so we brought that into our virtual environment so you now should be able to say pip3 list and see that not only was flask installed but a bunch of other dependencies that flask depends on so that's going to get everything we need for our project now if you want to get this project up in source control or share across numerous computers well it's recommended to basically make a list of all these dependencies and that way whenever someone downloads this project they can install those same dependencies so the way you can do that is you say pip3 freeze and then direct that output to a file known as requirements.txt so that's basically going to list this output if you don't want it to go to the file you can just see it so you can say pip3 freeze and it's going to give you what the output would look like then we're just directing it to this requirements.txt file so if you open that requirements.txt file this is what it's going to look like so that way when someone downloads this project they know exactly what they need to get then i believe when you need to download these later on all you would need to do is say pip3 install hyphen r requirements.txt and hit enter and you can see that we already met all of those requirements so we're good to go but if for some reason you didn't have one of those it would install it for you all right so that is a pretty good start but now we actually need a python file so let's go ahead and create a new file and we are going to just call this application dot py now for default behavior it's actually important to call it application.py this is what's going to allow it to connect to aws very easily however there are some configurations if you want to try to change that when we get to that point all right so this is going to be your application so what we're going to do is we're going to say from flask import flask so the first one is lowercase second one is uppercase cool now for flask applications you're going to see something that looks like this application is a new instance of flask and you're going to pass in underscore underscore name underscore underscore now we can refer to our application with this variable here you'll also often see it app however this is again a convention thing with aws so i'll talk about how to fix that later so let's go ahead and create a very simple endpoint so we say at application dot route and pass in the path in quotes so this is going to be the default web page and what happens when we visit that well we're just going to define a function you can call this whatever we'll call a hello world and what's this going to do it's just going to return a string saying something like sup oh and subscribe of course very professional stuff all right so now we have this application so now we want to run it locally just to make sure everything's connected right locally before we deploy it to amazon web services now we're going to do is we're going to say export flask app and set this equal to application dot py hopefully i got this right and that should temporarily add that to our path so we should just be able to say flask run alright cool so when we do that we get a warning this is a development server do not use it in a production deployment but we're not because we're going to just do this for local testing and then we're going to deploy to aws so what this video is doing is it's giving you the solution to this problem here all right so let's go check this on the web browser so i'll just open this up and hey take a look at that it looks like it's working it tells you to subscribe and it says sup so now i want to get this up in aws so go to aws and we're going to do this on the web page however you can also do it with their command line interface if you're into that kind of thing so i'm just going to go to my account go to the management console and the way we're going to deploy this is using elastic bean stock and here you can see my previous deployments in fact this was from a video i did about nine months ago i left it up because i was planning on working with it but i obviously haven't gotten too much done with that so i should probably delete these and save myself some money anywho let's go ahead and create a new environment and this is going to be a web server environment so hit select for this we're just going to say flask example which is the name of my project and scroll down here um a lot of the stuff we're not going to need to fill out the only thing we'll need to fill out is python and the python version 3.7 is fine and we're going to start with the sample application you're not going to upload your code although that is an option however i don't recommend it because i really recommend you store your code in github and have that automatically propagate to your server so let's go start with the sample application and i'll talk about how to do that continuous integration part in just a moment so let's create this environment and you're going to get a bunch of stuff in this output here obviously if you are not already on aws you'll need to create an account and stuff and you should get some level of freeness with elastic bean stock for like a year or something however just to warn you guys this is a paid service so it might cost you like 10 or 15 bucks a month after your free trial is over so don't forget about that pretty much anything in aws you should just assume it's going to cost money unless it specifically says it does not and they're really good at taking your money because they're not going to warn you as you're adding servers and changing stuff well well this is going to take a few minutes so let me take a moment just to share with you guys that i am currently working on a python course so if you want to know how to become a python developer or how to get better at python then i recommend you check out the link in the description because that's going to allow you to basically subscribe with your email and i'll be able to notify you whenever that course comes out so that's all i got end of shameless plug all right and there we go it is done okay so now we need to worry about this part where we get the code so again we're not going to upload our code what we're going to do is we're actually going to go to the services again and what i'm interested in is called code pipeline so this is part of a bigger project where you can you know do automated testing and deployments but the only thing i'm interested in is this taking our code from github and getting it on amazon web services so here's what we're going to do we're going to create a pipeline and give this a name we'll just call it again flask example and everything else is good so hit next now for the source provider here is all of the different sources of source code that you can use so we're going to go with github however you can also use bitbucket this is a service i used to use a lot back when github charged for private repositories anytime i want to use a private repository i'd use bitbucket but you can also use amazon s3 and so forth so let's just go with github so then what you need to do is connect to github connection name i don't know connection that sounds good so yeah you have to go through all these different processes so we're going to work with caleb curry that's my github account and if this is your first time you know you might get some pop-ups so go ahead and just accept all the permissions and then once you are here you're just going to select a repository we don't have a repository we need to go and take our code from our local machine and get that up on github which shouldn't take too long so what we're going to do is go back to our code exit out of our server just with control c and you need to make sure you have git if this doesn't cause something to pop up then you need to go install git we're going to do is say git init so that's going to initialize an empty git repository and again just confirm you are in the right directory so you're not getting anything too confused and what we want to do is we want to basically take all these files that we have and get them up on github but we don't want all of the files like i just said we only want certain ones basically the ones you can't generate so we don't need to include like for example the cache files and all that junk so what we're going to do is we're going to use a git ignore so to do this we can right click and say new file it's always the hardest button to find on those lists and we're just going to say dot get ignore all right so this is our get ignore and this is where you can put what files you want to ignore and my recommendation is just to find a get ignore template online so let's go back to our web browser and find one of those so we're just looking at python git ignore and this should do the trick so go ahead and copy all of these lines of code and that's going to be sure you don't push anything into your repository that you shouldn't push in there or that is unnecessary so we can paste that in here and you can customize this to your exact application for example these are the possible different names for the environment we got venv which is one of them that they put on here but we don't have all these other ones anyways i'm going to keep that git ignore as is it looks fantastic so now let's go back to our terminal and we can say git status and you can see now it's going to include that get ignore but it's going to ignore what was originally in there the virtual environment and the cached files so once we got all this ready i would consider this to be our initial commit so we'll just say git add all get commit hyphen m init and that's just the message so and it seems pretty descriptive right hit enter and now those are all on our repo now we just need to get it pushed up to github so let's go over to github and looks like we're already on github so what we can do is we can create a new repository and give it some name such as flask example and we're going to keep this public and don't click any of these because we are importing an existing repository that we just created locally so create this repository and we're going to take a look at these lines because we're not creating a new repository we're pushing an existing one i'm just going to take these one line at a time just to make sure everything is nice and clean so the very first thing is we add that destination on github then what we do is we create a branch main and uh just as like an fyi this used to be called master and i noticed something was different here but i couldn't figure it out and then um i saw that and i looked it up and you know has to do something with like slavery is bad whatever anyways we're going to paste that in there and this one as well i don't support slavery i'm just so regardless of what your opinion on the branch name is you could go ahead and name it whatever you want but just for consistency's sake i'm going to stick with what the examples given are so i'm going to call it main and then i'm going to push to the origin this web address our main branch so i did all that i just got a push so then we just say get push origin main now when we do it oh did i do that guys i'm going crazy anyways you should be able to refresh over here and there's all of our code awesome let's go back to the code pipeline all right so the repository we're looking for is flask example if for some reason it's not showing up there it might just because we just created it so you might just want to give it a second or if you have to do a refresh and get back to this point as for the branch we're going to go with main and we're just going to go with the default here so hit next and the build provider if you're using something like jenkins you can do all that stuff we're going to skip this stage and then deploy we're deploying to elastic bean stock and specifically the flask example that we created in the environment hit next and yeah just go through all the steps i'm sure you guys can do it so yeah we just went through all the steps and here we are so it's getting our code it's in progress once that it's done it's going to show up here as succeeded and it's going to go on to the deploy section which is getting our code actually to the web server which is currently in progress this is usually the slower one however i will just kind of warn you guys because i've messed this up a few times when you're coding something changing it locally and then pushing it to github it's automatically going to refresh this and get the new code and sometimes i look at this and i'm like oh okay it's exceeded it got the code and i'll go refresh the page and the changes aren't there yet that's because you have to wait for it to go through the entire deployment which can usually take a minute or two so this isn't exactly the most way to test development but it is a nice way to every now and again push to the server so people can test it out and you can just make sure everything is working off of your machine now the whole continuous integration continuous deployment thing can get pretty complex so for example you could have an environment to do testing and then an environment to deploy to so then you might want to you know push to the test server every night make sure your changes are good and then you know once a week push to the deployment server just as an example all right there it has succeeded so we can do is we can hit aws elastic bean stock that's going to bring up this application and then go to this url here and hey look at that we have this sup dot subscribe but the cool thing here is that this is not local meaning i could take this web address i could type it out on my phone if i'm feeling a little crazy or i could send it to someone to check out this webpage and you can also go into amazon and get a custom domain so then you could just have it like calebcurry.com or whatever it might be probably not that one though so just to see this process again let's go ahead and change our code and we're going to make this a little bit more formal hello there and we'll say good sir add them to the next commit and we're going to say small change to string and then you're going to say get push origin head get push origin main see i'm getting all mixed up alright so we got that on main and then what's going to happen is this is automatically going to grab that code you can see just now and it says small change to string and it starts that deployment process all right there we go it deployed so we should be able to go over here hit refresh and there you go there are your changes so if this is your first time doing something like this it's pretty cool feeling so congrats if it's not your first time then uh congrats anywho let's go back to our code and you'll often see flask applications done with app instead of application and i think just this is a thing with aws for example if i go ahead and change this to app and then we push these changes we'll just add a little message here breaking stuff hit push origin just mean got it all right now over here we'll check the pipeline it got the code and let's wait for this to deploy all right so it deployed now let's go ahead and do a refresh and oh it's broken so if you get to this point you can go ahead and go to elastic beanstalk and open your environment and there should be these logs on the side which you can request logs if you want to read through that and you can look up logging because i'm sure there's different tiers of that but honestly i don't really understand anything they're saying so let's just go ahead and take a look at our code and fix the problem so you can see we have app as the variable here and it needs to be application for aws so the problem is obviously a lot of people just like to use app for flask so the easiest fix is just to make an alias where app is equal to application so now these will both point to the same area of memory now there's also configuration for elastic bean stock so if you go into your environment and then go into uh environment [Music] configuration aha and then you should be able to go in and just change some of the stuff like just look for what what what's available here so yeah and you this is what i'm gonna do it's easier so then we'll just say get ad get commit gotta be very descriptive all right so i just pushed that and giving it a refresh you can see it's fixed all right so that's the basics if you want to build upon this then go ahead and develop out an api so take post requests and you know work with a database and maybe that's something i will do in another video coming up soon so please be sure to subscribe if you're interested in more python web development and yeah thank you so much i will see you all in the next one before you go if you have any issues and you work through them go ahead and post them in the comments with a solution so other people can figure it out that way this video is the most useful for people and we can all work together so thank you for watching again check out my link to my python course if you're interested and i'll see you in the next video [Music] you
Info
Channel: Caleb Curry
Views: 11,266
Rating: 4.9333334 out of 5
Keywords: flask aws, flask elastic beanstalk, flask deployment, ci cd, python aws, python elastic beanstalk, aws elastic beanstalk, amazon web services, elastic beanstalk
Id: 4tDjVFbi31o
Channel Id: undefined
Length: 22min 19sec (1339 seconds)
Published: Mon Nov 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.