How to deploy Python Flask application on AWS EC2 Instance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome to another video on my YouTube channel if you are looking for a place where you can host your Python and flask application for demonstration purpose and also you don't want to spend any money on it then you are looking at the Right video in this video you will learn how you can deploy your python flask API or web application on AWS ec2 instance for free and show it to your potential client and get freelance work so let's get started in order to follow along with this tutorial you need few things ready first you need your python flask application running and make sure you have latest requirements.txt file so that we can install the packages that is needed for that application to run second you need AWS account and make sure you verify your AWS account so that you can create ec2 instance next you'll need a filezilla or any such kind of application with which you can transfer content from your computer or local machine to any server it is not mandatory to use filezilla we can copy paste you know files into the ec2 instance but you know that will be not helpful in case if you have a bigger application once you have these things ready we are ready to deploy our python flask application on AWS now I will open my terminal and navigate to the folder where my code is and I'll open it into Visual Studio code I already have created a virtual environment for this and I'll run my code in my case it is python run dot pi and I'll click on this link so yeah I see all is well and that means my code is running successfully so I'll stop this I'll use peep freeze uh Arrow sign and then requirements.txt so this will freeze all the dependencies and write them into requirements.txt file so yeah it is there now I'll go to AWS and I'll search for ec2 I'll go to ec2 it will open ec2 dashboard here I'll click on launch instance here I'll write a test instance this will be name of my instance I will select Amazon Linux this is Amazon machine image and I left everything else as it is I'll then select T2 micro it is you know it is it is best suitable for you know demonstration purpose then in the key pair section I'll click on create new pair I'll say test instance key I'll click on create key pair yeah it will download a Pam file next I'll select allow SSH traffic from anywhere then you can you know select allow HTTP traffic from internet as well then I'll left the remaining configuration as it is and I'll create launch instance this will launch the instance and we can go to view all instances and this is my instance now I would like to connect with this instance so I'll click copy this public ipv4 address I'll go to my terminal and before that what I'll do that I will copy paste the file the Pam file we just downloaded into this directory so this is my file and I'll paste it here so if I LS I can see that test instance underscore key dot Pam file so I'll do SSH ec2 hyphen user at the rate the IP address we just copied let me make sure I did copy that okay and then I'll paste it here I'll hit enter SSH will ask me to add this fingerprint I'll say yes it will say permission denied cause uh my computer doesn't have the permission to access that server so what I'll do I'll do SSH hyphen I and then provide the path or the file where the Pam file is and then I'll say ec2 hyphen user at the rate the public IP address and I'll hit enter it will still say that you don't have the permission and this is a common problem so I'll do CH more 0 400 and then I'll provide the path to the Pam file now if I try to login into the ec2 instance hopefully I'll be successful yeah you can see we have login into the ec2 instance now let's make sure the python version here so if I write python then Python 2.7 is there and if I write Python 3 then python 3.7.16 is there so I think we don't have to install anything now let me go to filezilla and let me connect to the instance through filezilla here I need to open the site manager I'll create a oh okay there is one YouTube demo site is there so I'll come here and I'll provide the public IP address and I'll select the here you need to select SFTP provide the public IP then select key file uh user is easy to user and I'll browse and select the file we have downloaded it is python AWS deploy and project code and I'll need to change to Pam file and here is the file now if I click ok and then if I go here and if I go to YouTube demo yeah I want to connect yeah you can see we are now into the server now I'll create a folder and enter into that folder let's say demo underscore app here on my left side in my local computer I will navigate to the folder where my code is so it is on desktop YouTube and and inside python AWS deploy and project folder so this is my code so I'll copy everything I'll not copy the environment file oh sorry environment folder so I need this I need run file yeah I need these files and I'll click on upload now these files are into the demo app directory we can go to this terminal we can do LS and indeed there is a demo app directory I'll CD into demo app I'll do LS so okay all the required files to run the application is there so what I'll do first I'll do Python 3 hyphen m v e and V v e n v e and V so v e n v is python module and the last v e and V is name of my virtual environment I'll create a virtual environment so now when I LS it will show that virtual environment so I'll source into v e n v bin and then activate so now you can see I am inside my virtual environment I'll do pip install iPhone R and from the requirements.txt file so this will install all the required dependencies [Music] yeah don't worry about this command it just says that you need to upgrade pip and if you want to you can do it I'll do it as well so now our application is ready to run so I'll say python run dot pi and you can see the application is running on Port 5000. so now if I go to my browser and copy this public address followed by 5000 Port hopefully we will see all is well I think it is not showing uh this all is well message and there is a reason for that let's solve that problem so we'll go to AWS under security section you will see security groups I'll click on that Security Group I will add it inbound rules yeah for SSH it is okay I don't need this we have created that I'll click on ADD rules I'll select custom TCP I'll select the port on which my python application is running I'll select this 0.0.0.0 slash zero I'll save the rules I think the rules are now saved now if I click on this yeah you can see we got all is well message so you can see we got indeed a request here so the application is now successfully deployed on AWS ec2 instance there are still few things that you can try in order to make it more robust so for example you can run the application using gunicon and then run app yeah so this will but you know this will run the PO uh the application on Port 8000 and if we try to you know uh call it on 8000 we will you know not get any response because port number is not exposed so what we can do is that uh you can say unicorn and then there is a bind and then there is a 0.0.0.0 followed by the port number and yeah I think uh this will work oh sorry guni Khan yeah you can see the application is running on Port 5000 localhost so now I can refresh this and you can see we got the response uh you can also you know run the same script using nginx for you know better uh load handling and everything also you can run this gunicon in Demon mode so that you can close this terminal and still application is running also you can try process manager 2 to run this python file and it will be you know running in the background so I hope this is helpful to you and I hope you have learned how to deploy your python flask application on AWS ec2 instance in case you face any issue following this tutorial please write it down into the comment section of this video and I'll get back to you if you like my work please consider subscribing to my channel it helps thank you peace
Info
Channel: Raj Kapadia
Views: 13,226
Rating: undefined out of 5
Keywords: ec2, aws ec2, aws ssh public key, ec2 deploy flask, ec2 deploy python flask, ec2 deploy flask application, ec2 deploy flask app, deploy python flask app, python flask, deploy python app, raj kapadia, python flask app ec2, ec2 python flask app, Python flask deploy tutorial
Id: 5gSG5jwOJSY
Channel Id: undefined
Length: 14min 45sec (885 seconds)
Published: Mon Feb 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.