augusthottie
Posted on August 2, 2024
Overview
This article walks you through setting up a messaging system using Flask, RabbitMQ, and Celery. The system handles asynchronous tasks like sending emails and logging timestamps. You can host the application on an AWS EC2 instance and use screen
to keep it running persistently, while exposing it to the internet using ngrok.
GitHub Repo
For code details visit my repo ⬇️:
Table of Contents
- Features
- Technologies Used
- Installation
- Configuration
- Running the Application
- Endpoints
- Logging
- Using ngrok
- Hosting on AWS EC2
Features
-
Send Emails: Use the
?sendmail
parameter to send emails via SMTP. -
Log Timestamps: Use the
?talktome
parameter to log the current time. -
View Logs: Access application logs via the
/logs
endpoint. - Asynchronous Tasks: Manage tasks asynchronously with RabbitMQ and Celery.
Technologies Used
- Flask: A lightweight framework for building web applications.
- Celery: An asynchronous task queue for managing background tasks.
- RabbitMQ: A messaging broker that handles message queuing.
- SMTP: Protocol used for sending emails.
- ngrok: A tool to expose your local server to the internet.
Installation
Prerequisites
- Python 3.7 or higher
-
pip
(Python package installer) - RabbitMQ server installed and running
Step 1: Clone the Repository
First, clone the repository and navigate to the project directory:
git clone https://github.com/AugustHottie/devops-stage3.git
cd devops-stage3
Step 2: Create a Virtual Environment
Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
Step 3: Install Dependencies
Install the necessary packages:
pip install -r requirements.txt
Configuration
- Set Up Environment Variables
Create a .env
file or set the variables directly in your shell:
MAIL_ADDRESS=your-email@gmail.com
APP_PASSWORD=your-google-app-password
LOG_FILE_PATH=/var/log/messaging_system.log
- Ensure RabbitMQ is Running
Start the RabbitMQ server:
sudo systemctl start rabbitmq-server
Running the Application
Step 1: Start the Celery Worker
Open a new terminal, activate your virtual environment, and start the Celery worker:
celery -A app.celery worker --loglevel=info
Step 2: Start the Flask Application
In another terminal window, run the Flask application:
python app.py
Endpoints
Endpoint | Description | Example Usage |
---|---|---|
/ |
Main route to send emails or log time | http://localhost:8000/?sendmail=your_email@example.com |
/ |
Main route to log the current time | http://localhost:8000/?talktome |
/logs |
View application logs | http://localhost:8000/logs |
Logging
Logs are saved to the path specified by LOG_FILE_PATH
. Make sure the application has permission to write to this location.
Using ngrok
To expose your local application to the internet, follow these steps:
- Download and Install ngrok
wget https://bin.equinox.io/c/4b0e5f0d1d6e/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
- Add Your ngrok Authtoken
Sign up or log in to your ngrok account to get your authtoken. Once you have it, run:
./ngrok authtoken your_ngrok_auth_token
Replace your_ngrok_auth_token
with the token you received from the ngrok dashboard.
- Expose Your Flask Application
./ngrok http 8000
- Use the Provided ngrok URL
Use the URL provided by ngrok to access your application externally.
Hosting on AWS EC2
Step 1: Launch an EC2 Instance
- Log in to AWS Management Console and navigate to EC2.
- Launch a new EC2 instance with your preferred configuration.
- Configure security groups to allow HTTP (port 80) and custom TCP (port 8000) traffic.
Step 2: Connect to Your EC2 Instance
Use SSH to connect to your EC2 instance:
ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-dns
Step 3: Install Dependencies on EC2
- Update the package list and install necessary packages:
sudo yum update -y
sudo yum install -y python3-pip
- Clone the repository, create a virtual environment, and install dependencies:
git clone https://github.com/AugustHottie/devops-stage3.git
cd devops-stage3
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
- Ensure RabbitMQ is installed and running:
sudo yum install -y rabbitmq-server
sudo systemctl start rabbitmq-server
Step 4: Set Up screen
to Persist the Application
-
Install
screen
if it is not already installed:
sudo yum install -y screen
- Start a new screen session:
screen -S myapp
- Run the Flask application within the screen session:
python app.py
Detach from the screen session by pressing
Ctrl+A
, thenD
.To reattach to the screen session:
screen -r myapp
Step 5: Set Up ngrok on EC2
- Download and install ngrok:
wget https://bin.equinox.io/c/4b0e5f0d1d6e/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
- Add your ngrok authtoken:
./ngrok authtoken your_ngrok_auth_token
Replace your_ngrok_auth_token
with the token you received from the ngrok dashboard.
- Expose your Flask application using ngrok:
./ngrok http 8000
- Use the provided ngrok URL to access your application externally.
This guide should help you get your messaging system up and running both locally and on an AWS EC2 instance. Feel free to share your feedback or suggestions in the comment section! 🚀
Posted on August 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.