Deploying an NLP Flask App on AWS EC2

Joyce Ndichu
3 min readOct 23, 2024

--

Introduction

Deploying a Flask-based Natural Language Processing (NLP) app on AWS EC2 involves several steps, from setting up the instance to configuring the web server. This guide walks through everything you need to launch your NLP app on AWS EC2, focusing on Python, Flask, Gunicorn and systemd.

Prerequisites

1. An AWS account
2. A key pair for SSH access
3. Basic command line knowledge

1. Setting Up the EC2 Instance

To begin, log in to your AWS Management Console and navigate to the EC2 Dashboard. Here’s a simple breakdown:

1. Launch a new EC2 instance.
2. Choose an Amazon Machine Image (AMI), such as Amazon Linux 2.
3. Select t2.micro for the instance type, which is suitable for small-scale apps.
4. In the security group, ensure SSH and HTTP/HTTPS access is allowed.
5. Use your key pair to access the instance.

2. Cloning Your Repository

Before cloning your repository, ensure that the security group associated with your EC2 instance has the necessary inbound rules set up, especially for SSH access (port 22).

Rule for SSH Access:

  • Port: 22
  • Protocol: TCP
  • Source: 0.0.0.0/0
  • Description: SSH access

2.1 Set the Permissions for Your SSH Key
First, ensure that your SSH key has the correct permissions. The private key should be readable only the owner. Run the following command on your local machine to set the correct permissions:

chmod 400 /path/to/your/keypair.pem

2.2 Connect to the EC2 Instance via SSH
After setting the permissions, connect to your EC2 instance:

ssh -i /path/to/your/keypair.pem ec2-user@<your-ec2-public-ip>

2.3 Update the System
Once you’ve connected to the instance, update your system’s packages to ensure everything is up to date:

sudo yum update -y

2.4 Install Git
If Git is not already installed, install it using the following command:

sudo yum install git -y

2.5 Clone Your Repository
Clone your project repository and navigate to the project directory:

git clone <github link>
cd nlp-project(the repository name)

3. Installing Python and Dependencies

3.1 Install Python and pip
You’ll need Python and pip installed on your instance:

sudo yum install python3 python3-pip -y

3.2 Set Up a Virtual Environment
Next, install virtualenv and create a virtual environment for your Flask app:

sudo pip3 install virtualenv
virtualenv venv
source venv/bin/activate

3.3 Install App Dependencies
Now, install the required dependencies for your app:

pip install -r requirements.txt

4. Configuring the Flask App

Ensure your Flask app is properly set up with environment variables and any necessary configurations.

5. Creating a WSGI File

To allow Gunicorn to serve your app, create a `wsgi.py` file with the following content:

from app import app

if __name__ == '__main__':
app.run()

6. Setting Up Gunicorn

Install Gunicorn to act as a Python WSGI HTTP server:

pip install gunicorn

7. Creating a Systemd Service

To ensure that Gunicorn runs your Flask app in the background and automatically restarts if the app crashes, you’ll need to create a systemd service. This will allow your application to be managed like a standard service on your Linux system.

7.1 Create the Service File

Open the terminal on your EC2 instance and create a new service file using a text editor like nano:

sudo nano /etc/systemd/system/nlp_flask_app.service

Add the following configuration to the file, adjusting the paths as necessary to match your application’s directory structure:


[Unit]
Description=NLP Flask App
After=network.target

[Service]
User=ec2-user
WorkingDirectory=/home/ec2-user/nlp-project
ExecStart=/home/ec2-user/.local/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 wsgi:app
Restart=always

[Install]
WantedBy=multi-user.target
  • User: The service runs as ec2-user, the default user for Amazon EC2 instances on Amazon Linux.
  • WorkingDirectory: Points to the project root directory (/home/ec2-user/nlp-project).
  • ExecStart: Starts the Flask app with Gunicorn, using --workers 3 for handling multiple requests and --bind 0.0.0.0:5000 to listen on all interfaces on port 5000.
  • Restart: Ensures the application automatically restarts if it crashes, enhancing service resilience.
  • [Install]: Specifies that the service should start in multi-user mode (WantedBy=multi-user.target), common for Linux services.

8. Opening Required Ports

To allow external traffic, navigate to your EC2 instance’s security group and add the following inbound rules:

Rule for Application Access:

  • Port: 5000
  • Protocol: TCP
  • Source: 0.0.0.0/0
  • Description: App access

Rule for HTTP Access:

  • Port: 80
  • Protocol: TCP
  • Source: 0.0.0.0/0
  • Description: HTTP access

Ensure these rules are correctly set up in the security group associated with your EC2 instance to allow proper access to your application.

9. Starting and Enabling the Service

Enable and start the Flask app service:

sudo systemctl daemon-reload
sudo systemctl start nlp_flask_app
sudo systemctl enable nlp_flask_app

10. Accessing Your Application

Once the service is running, you can access your app by navigating to:

http://<your-ec2-public-ip>:5000

11. Monitoring and Logs

Monitor the logs of your application to ensure it’s running smoothly:

sudo journalctl -u nlp_flask_app.service -f

--

--

Joyce Ndichu
Joyce Ndichu

No responses yet