This content originally appeared on Level Up Coding - Medium and was authored by Stefano Giannattasio
In this short guide, I will try to show you how you can easily deploy a flask application on an Amazon EC2 instance and make it reachable from anywhere.
Flask is a micro web framework written in Python. It does not require particular libraries and makes building a web application quite simple. For these reasons, it may be a valid solution to build a simple web application.
Once our own application was built and subsequently debugged locally, the question is “where can I deploy it without too much effort?”. Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. You can easily rent and manage a virtual machine and only pay for the time it takes. There is also a free tier valid for one year since your sign-up on AWS. All this makes EC2 a valid solution for this purpose.
The guide will follow these steps:
- How to create an EC2 instance
- How to deploy your own flask app on the created instance
1. How to create an EC2 instance
After creating a new account on AWS, you have to search “EC2” under “services” and then “compute” section. Once EC2 dashboard is open, click on “instances” in the list view on the left and then on Launch instances.
In the next page you can choose the machine image you prefer. Let’s choose Ubuntu 20.04 LTS which is Free tier eligible.
Choose a t2.micro if you have a free tier account to get 750 hours of usage for free. After that, the following default settings may be fine for an application without special needs.
Before launching the instance it is important to configure the security group: if you have not one yet, you need to create a new one and you can change its name for a better association with your application. After that, beyond port 22 for SSH connection, it is necessary to open a port for the HTTP protocol. If you want to release your application, the right choice is to open port 80, but an application needs root privileges to access it, so if you have not them or you don’t want to release the application but only deploy it on the server maybe for testing, you can choose the port 8080.
In the review section you can check the details of your instance and if all is fine then click on the Launch button. To access your virtual machine via SSH you have to create a new key pair and download the generated .pem file (click here to learn more about pem files). It is absolutely fundamental that you store this file in a secure place because it is the only way to access your virtual machine, furthermore if it ends up in wrong hands someone can access your instance!
After the Key Pair download, you can finally launch the instance!
Being free when associated with a running instance, it is a good idea to create an elastic IP and use this IP to point the server. To do that, you have to go to the “Elastic IPs” section, allocate a new Elastic IP and associate it to your instance. The main benefit of having an Elastic IP is that if an instance is shut down you can easily associate it to another instance to keep your web application reachable.
2. How to deploy your own flask app on the created instance
Once created your ec2 instance and verified it is running, let’s see how to access it from bash and copy the files of the flask application into the Ubuntu server.
Deploy to Production
In this guide we are talking about deployment, but especially in Flask applications it is important to be able to distinguish between this term and production. When running publicly rather than in development, you should not use the built-in development server. The development server is provided by Werkzeug for convenience but is not designed to be particularly efficient, stable, or secure.
A better choice is instead to use a production WSGI server, for example Waitress. If you don’t want to publicly release your web application, you can skip the rest of this subsection, otherwise I recommend you to install Waitress and use this WSGI server instead of the built-in one.
$ pip install waitress
In app.py
If you don’t want to use Waitress, just use app.run function as mentioned above. If in your virtual machine you have opened port 8080 instead of port 80, change the port number in the function.
SSH into the virtual machine
First of all, enter in your bash and locate the .pem file previously downloaded. Type chmod 600 ./<PEM_NAME>.pem to restrict read and write permission to the private key, so only the user/owner can read and write, but no one can execute. After that, you can log into the virtual machine typing
$ ssh -i path/<PEM-NAME>.pem ubuntu@<IP-ADDRESS>
Virtual machine setup
Now you should be inside the ubuntu shell and you can proceed to install the dependencies. In particular, it is necessary to install
- python3
- pip3
- tmux
- htop (to display crucial system metrics)
To do this you just have to type few lines.
$ sudo apt update
$ sudo apt install python3 python3-pip tmux htop
Now you can create a new folder that will contain your application.
$ mkdir deployedapp
Transfer your files to the EC2 instance
Open a new tab of your bash to work on your local machine, so you can keep active the virtual machine session. If you have not a requirements.txt containing all your application dependencies yet, you can easily create it: move into the folder where your application is located and type
$ pip freeze > requirements.txt
If you used a virtual environment, you obviously need to activate it first.
Now you can transfer your application to the remote host.
$ scp -ri path/<PEM-NAME>.pem path/to/your/application ubuntu@<IP-ADDRESS>:/home/ubuntu/deployedapp
Deploy the application
Now go back to the virtual machine shell. Access it via SSH again if you have disconnected. Check if your application folders and files are now inside the deployedapp folder as it should be.
Use tmux to create a new session.
$ tmux new -s flasksession
Now a new tmux session has been opened. Install your requirements (creating a new virtual environment for each project is always a good idea, but in this case if your flask app will be the only application running on the server, you can avoid doing that).
$ pip3 install -r requirements.txt
The TCP/IP port numbers below 1024 require root privileges to run server on them, therefore if you use port 80 you need to use the keyword sudo .
In your deployed app folder
$ sudo python3 app.py
Now your server is running. Detach the tmux session with Ctrl-b-d (press Ctrl-b followed by d). To attach the session again you can type
$ tmux attach -t flasksession
To see all the active sessions
$ tmux ls
Finally you can press Ctrl-c to logout the virtual machine, while your application will keep running!
References
How to Deploy a Flask Application on Amazon EC2 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Stefano Giannattasio
Stefano Giannattasio | Sciencx (2021-05-06T14:45:00+00:00) How to Deploy a Flask Application on Amazon EC2. Retrieved from https://www.scien.cx/2021/05/06/how-to-deploy-a-flask-application-on-amazon-ec2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.