Calculator App Deployment on AWS EC2 Flask and Gunicorn
App Deployment on AWS EC2 - "100 days of MLOPS"
Table of contents
- Prerequisites
- Step 1: Launch an EC2 instance
- Step 2: Connect to the instance
- Step 3: Clone the repository
- Step 4: Update the system
- Step 5: Install the requirements
- Step 6: Run the app
- Conclusion
- Now run the app using gunicorn3 and Nginx.
- Step 1: Install gunicorn3
- Step 2: Create the Nginx configuration file
- Step 3: Enable the Nginx configuration
- Step 4: Restart Nginx
- Step 5: Run the app
- Conclusion
In this tutorial, we will be deploying a Flask Calculator app on AWS EC2 instance. We will clone the repository, update the system, install the requirements and run the app using a public IP.
Prerequisites
Before we start, you should have the following:
An AWS account
Basic knowledge of AWS EC2 instances
Basic knowledge of the command line interface
Step 1: Launch an EC2 instance
Log in to your AWS account and go to the EC2 dashboard.
Click on the "Launch Instance" button.
Choose an Amazon Machine Image (AMI) that is compatible with your application. For instance, select an Ubuntu Server AMI.
Choose an instance type according to your needs.
Configure the instance details, such as the number of instances, network settings, and storage.
Add tags to your instance for better identification.
Configure the security group to allow HTTP and SSH traffic.
Review and launch the instance.
Step 2: Connect to the instance
Open your terminal and navigate to the directory where your private key is stored.
Run the following command to change the permissions of your private key file:
chmod 400 {path_to_private_key}
- Connect to the instance using SSH with the following command:
ssh -i {path_to_private_key} ubuntu@{public_ip_address}
Step 3: Clone the repository
- Install Git on your instance with the following command:
sudo apt-get update
sudo apt-get install git
Navigate to the directory where you want to clone the repository.
Clone the repository with the following command:
git clone {repository_url}
Repository - https://github.com/sourangshupal/my_calculator
Step 4: Update the system
- Update the system with the following command:
sudo apt-get update
sudo apt-get upgrade
Step 5: Install the requirements
Navigate to the directory where the Flask Calculator app is located.
Create a virtual environment with the following command:
python3 -m venv venv
- Activate the virtual environment with the following command:
source venv/bin/activate
- Install the requirements with the following command:
pip install -r requirements.txt
Step 6: Run the app
- Run the app with the following command:
python3 app.py
- Access the app using the public IP address of your instance in your web browser.
Output
Conclusion
In this tutorial, we have covered the steps to deploy a Flask Calculator app on AWS EC2 instance. You can now clone your repository, update the system, install the requirements, and run the app using the public IP address of your instance.
Now run the app using gunicorn3 and Nginx.
Step 1: Install gunicorn3
- Install gunicorn3 with the following command:
sudo apt-get install gunicorn3
Step 2: Create the Nginx configuration file
- Create the Nginx configuration file with the following command:
sudo nano /etc/nginx/sites-available/flask_app
- Paste the following configuration into the file:
server {
listen 80;
server_name {public_ip_address};
location / {
proxy_pass <http://localhost:8000>;
}
}
Replace
{public_ip_address}
with the public IP address of your instance.Save and close the file.
Step 3: Enable the Nginx configuration
- Enable the Nginx configuration with the following command:
sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled/flask_app
Step 4: Restart Nginx
- Restart Nginx with the following command:
sudo systemctl restart nginx
Step 5: Run the app
- Go to the
my_calculator
directory and run the app with the following command:
gunicorn3 --bind localhost:8000 app:app.py
- Access the app using the public IP address of your instance in your web browser.
Output
Conclusion
In this tutorial, we have covered the steps to deploy a Flask Calculator app on AWS EC2 instance using gunicorn3 and Nginx. You can now clone your repository, update the system, install the requirements, and run the app using the public IP address of your instance.