Deploy Python Applications To AWS Lightsail
Key Takeaways
This video demonstrates how to deploy Python applications to AWS Lightsail, covering the setup of a virtual private server, installation of dependencies, and configuration of a professional server using Gunicorn and systemd. The tutorial uses tools such as AWS Lightsail, SSH, pip, and apt to deploy a Flask application.
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to easily deploy python applications to AWS light sale so let us get right into [Music] it all right so we're going to learn how to deploy a python application to AWS light sale in this video today now light sale is an AWS service that allows us to rent a VPS so a virtual private server and it's a very simple and straightforward way to deploy a python application so this is not going to be very complicated today and you can deploy whatever application you want a simple python script like a chatbot listening at a port a flask application ajango application a fast API application it doesn't matter but if you want to deploy the exact same application that I'm going to deploy here for demonstration purposes you can go to my GitHub repository you will find a link in the description down below uh to this YouTube tutorials repository here you want to go to to-do list app flask this is the thing that I'm going to deploy today but as I said you can deploy a hello world application a chat bot it doesn't matter uh the process is the same so what you want to do is you want to go to the AWS console I'm going to assume that you already have an account I'm not going to guide you through that process and once you are in the AWS console you want to look for light sell so you want to search it here in my case I have a link because I already used it recently and here now we want to create a new instance so I'm going to click here on create instance in my case it already suggests the in instance location Germany um I'm going to pick this one because I'm living in Austria which is uh close to Germany so I'm going to just uh go with this one but you can customize this if you want to and down here now you can make a couple of choices so you can choose the platform the operating system and there's also blueprints here so for example I'm going to go here now with Linux and if you want to go with Linux you have uh you can choose here between WordPress and no JS and also D Jango even if you want to um but you can see there is no flask option so since I'm going to deploy a flask application I'm going to go with the operating system only and I'm going to go with a buntu here I have one uploaded already but how do you do that what you do is you open up your command line you go to some directory and you run the command s sh. Keygen this command allows you to generate a public and private key pair so what you want to do is you want to um specify a location where you want to save this the default here is going to be the SSH folder of your user and the default name is going to be ID RSA in my case what I did is I called this home neural 9 . SSH and then ID RSA AWS now I'm going to call this now aws2 um you can also choose a passphrase which I would recommend I'm not going to choose one now here for this one um then you want to use the same passphrase again and then you have this key in your um in your directory so I can go to SSH and I can say LS and I'm going to grab here to not show all of my keys the ID RSA aw s Keys you can see that I have uh the ID RSA AWS key uh one the private and the public key and now I have the second one as well so this is what you want to do you want to generate these keys and you will have one private key one secret key and uh one public key you never want to share the private key or the secret key this is something that should only be present on your machine or on your machines um you want to upload the public key so what you do is you generate a key pair the way I did it right now and then you upload to to this um to this creation process here you upload the public key so you're uploading the ID RSA AWS do pup key to this um dialogue here um once you have that you can then also say you want to enable automatic snapshots for example every time every day at a specific time you want to make make a backup um and then you can choose the plan that you want to go with so this depends of course on what type of application you're running I'm just going to go here now with the the $7 plan where you have 1 GB of RAM you have two uh virtual CPUs 40 GB of SSD storage and uh two terabyt of transfer so you can use uh two terabytes of um data transfer here with this plan um it doesn't matter which one you choose it just matters that it's enough for you uh or to support your application I'm just going to go with this one now uh then we can choose a name for our machine let's call this buntu tutorial um um and then we can create the instance so this instance now will go live after some time and we will be able to easily connect to this instance because we provided a public key and we have the private key it's important the private key has to be located the private and publicy have to be located in your SSH folder otherwise you need to specify the path to them uh manually but if they are in your SSH folder on Linux you can just go and say SSH into the server it's going to recognize the public key or and the private key uh and you can easily do that so I can restart or I can reload the page here uh it seems to be live so what I'm going to do now is I'm going to open up my terminal and I'm going to say I'm going to go first of all into or actually I don't have to go into it you can see here you have an IP address so I can actually go ahead and copy this IP address and say SSH Ubuntu this is the user that you want to use at IP address now in my case here it asks me now if I want to um connect because there's a fingerprint here I can say yes and then I can use my password for my SSH key uh because for the key that I'm actually using I specified now the um I specified a passphrase and I need to enter this passphrase to be able to use it now I'm connected onto the server I can clear this lck here and now I have the command line access to the server so what do I want to do here now on the server to deploy the application well I need to get the application onto the server now we can check if python is installed already in this case it is it's python 3.10 which is fine and for our project now for our to-do list application we need to make sure we have a requirements txt file now this is not a necessity you can also just manually install the uh dependencies but you want to make sure that you have all the things you need to make this application uh to run this application on the server so in this case what I'm going to do now is I'm going to navigate here I'm going to open up a second command line I'm going to navigate to my uh folder which is the current folder and in here I have my to-do list app flask so what I'm going to do is I'm going to use SCP to upload this to the server how I'm going am I going to do that I'm going to say SCP dasr for recursive because it's a a directory here to Du list app flask and then Ubuntu add the IP address that we just used which is this one here add this and then colon slome buntu I want to place this at this directory I'm going to do this then this is going to upload the files and now if I run LS on the server you can see I have my to-do list application flask on the server now on the server of course I don't have flask installed so what I want to do is I want to say pip 3 install dasr requirements txt uh in this case now it says I don't have Pip installed so I need to say pseudo app install Python 3 pip but this is probably going to lead to the next problem which is it doesn't have the uh repositories up to date so we're going to say pseudo apt update and then we're going to run the same command again so pseudo apt update to get all the repositories then we're going to download the PIP tool with the PIP tool we're going to install flask and then we can run our application um now first of all what we're going to do here now is we're going to say say the same thing as before there you go install pip uh another thing to consider is what kind of port or what port you want to run your application on now if you just use the Run command if you just want to run your flask application in debug mode you can do that at Port 80 and access you can access the server or the application at this port but you're going to have troubles running this as a service and so on I'm going to just restart these Services now here um so it makes sense to run the application not on Port 80 because this requires usually root privileges if you want to run on a port less than 1,24 um because of that you want to be running on a higher Port like something 8080 8,000 or something like this and for this you need to set up a firewall option but we're going to talk about this here in a second so now I'm going to use pip to install everything in requirements txt file this doesn't have to be just flask it can be psychic learn if you have a machine learning or a tensorflow if you have a machine learning application um but once you have everything installed you should be able to run Python 3 um app.py uh or probably pseudo- e Python 3 apppi and then it's listening here on 000000 so it's buying on all the addresses um and what we should be able to do now is we should be able to open this and you can see my application is available at this IP address if you right now were to type in this IP address probably when you're watching the video no longer but if you were to type this IP address while I'm recording this video you would be able to see this application as well and I can use the functionality of this application here and I can also see that it's running uh on the server I can also close it um and now it's gone so this is of course not the best practice way or in any way a good way to do this because first of all we're running this in debug mode we're running this with the um oh I don't have new Vim here so I'm going to use Vim uh we're running the app.run command we're not doing it professionally because professionally we would use something like uh gunicorn or unicorn I'm not sure how it's pronounced uh which we're going to do here in a second and also we want to be running this as a service because otherwise if I log out of the terminal if I end a session it's also going to end the server so it's not going to be running if I'm not in the command line so what we want to do is we want to set up a service how do we do that first of all we get out of this file here uh and I'm going to say now uh pip 3 install gunicorn or unicorn uh this is going to allow us to serve this as an actual server um also as I mentioned we want to have a certain firewall rule we want to go to networking here and we want to set up or actually is this the proper way to do that let me just double check where I have to do this uh instances manage there you go networking and here now I want to add a rule to the file wall right now as you can see there's the SSH axis there's the htttp axis uh I also want to add now the rule that I want to be able to get traffic at Port 8000 so custom TCP 8000 and then I can just go ahead and uh create this Rule and now this is going to also be possible before that I'm not going to be able to Target Port 8,000 with the application which I want to do so we're going to say let's go to the app or actually we don't need to go to the app because we're not going to be uh running it like this what we want to do is we want to use um gunicorn to run this so I want to run this as a service what I'm going to do is I'm going to create a new file in ETC systemd system and here now I'm going to create a service and I'm going to call the service to-do app. uh service and in this file now I'm going to specify certain things first of all I'm going to say in square brackets unit is going to be having the following Fields here description is going to be equal to too application after is going to be equal to network Target then I'm going to say here service in square brackets I'm going to say user equals auntu which means I'm not going to run this as root to ser is going to be ran by the user obuntu um the working directory is going to be the following slome obuntu uh maybe to prevent problems here oh I have a problem because I didn't open this in root so let's actually leave this here and also let's rename the folder because wides spaces are always problematic I'm going to move the to-do list application to a folder called do app so I just renamed it here um and now I'm going to run the same thing but I'm going to do this with uh pseudo permissions I'm going to say pseudo- e um this and I'm going to say now again unit description equals to do application let me just see if I can save this works okay um then after is equal to network Target and then we Define the service here user equals one to capital u here and then we want to say uh working directory is equal to slome SL obuntu SL too app and then the EXC start the command basically that we want to run is home um buntu local /bin SLG unicorn which is the path to our gunicorn uh binary here if you have a different path of course specify the path to the application here uh - W for workers we're going to use just one worker the reason here I want to use just one worker is because this specific app the to-do list application stores the to-do in uh the ram in in the in the process itself so it doesn't use a file or a database to do that because of that if you have four workers you're going to be connected to one of them and it's not going to have the previous states of the other on so if you have an application with a database you can use multiple workers in this very specific case of the to-do list application that has just a list in the process itself one worker is uh the way to go I'm going to say I want to bind this to 000000 Port 8,000 and I want to use the app py file and the app of the app py file so app colon app restart is going to be set to always I always want to keep the service running and I'm going to say this in square brackets again wanted by equals multi-user Target so I'm going to save this now and what I'm going to do is I'm going to say system pseudo system CTL Dam and reload then I'm going to say system CTL start to do app uh like this and then we can check the status of the to-do list uh of the to do app and you can see it's active and running I can also enable it to be enabled at startup so whenever the server a server restarts it's going to also start the to-do list application um and now actually I should be able to uh specify here the port 8,000 oh not 80,000 but 8,000 so let's just try again here on this port this should not be working uh I want to do this on Port 8000 and now I get this to-do list application here as you can see so this is how you do that you run this as a service because even now if I disconnect um even if I disconnect now from this um machine you're going to see that this is still continuing to run so I can still delete stuff I can still add stuff and so on and this is how you basically deploy a python application onto AWS light saale so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
Today we learn how to deploy a Python application to AWS Lightsail.
AWS Lightsail: https://lightsail.aws.amazon.com/ls/webapp/home
Flask App Code: https://github.com/NeuralNine/youtube-tutorials/tree/main/Todo%20List%20App%20Flask
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Tools for PMs
View skill →Related Reads
📰
📰
📰
📰
Morris Pre Order Traversal
Dev.to · Jaspreet singh
Advanced Stack ApplicationsData Structures and Algorithms Deep‑Dive — Advanced Stack Applications…
Medium · Programming
The Minecraft anvil is a tree-cost optimization problem in disguise
Dev.to · Mark
KMP Algorithm (Knuth-Morris-Pratt): The Smart Way to Perform String Matching in O(N)
Dev.to · Jaspreet singh
🎓
Tutor Explanation
DeepCamp AI