This is a Flask API that connects to a PostgreSQL database hosted on AWS RDS and can be deployed on an EC2 instance.
- AWS account
- AWS CLI installed and configured on your local machine
- Python 3.7+ installed
- Clone the repository and navigate into the project directory.
git clone https://github.com/your-repo/flask-api
cd flask-api
- Install dependencies:
pip install -r requirements.txt
- Configure environment variables for database connection. Create a
.env
file with the following format:
DB_HOST=<your-rds-endpoint>
DB_PORT=5432
DB_NAME=<your-database-name>
DB_USER=<your-username>
DB_PASSWORD=<your-password>
- Create the
items
table in your PostgreSQL database:
CREATE TABLE items (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
- Go to the RDS section in the AWS Console and create a PostgreSQL instance.
- In Connectivity & Security:
- Enable Public Access.
- Configure the VPC security group to allow inbound traffic on the PostgreSQL port (default is 5432) from your EC2 instance.
- After the instance is created, note down the endpoint and port.
-
Go to the EC2 section in the AWS Console and launch a new instance:
- Select an Amazon Linux or Ubuntu instance.
- Choose an instance type (t2.micro is recommended for testing).
- Configure the security group to allow inbound traffic on ports 22 (SSH) and 5000 (Flask app).
-
SSH into your EC2 instance using the key pair you specified when creating the instance:
ssh -i /path/to/key.pem ec2-user@your-ec2-public-ip
- Update packages and install necessary software on EC2:
# Update the package lists
sudo apt update
# Install Python 3 and pip
sudo apt install python3 python3-pip -y
# Install virtualenv (recommended)
pip3 install virtualenv
- Clone your repository on EC2:
# Clone the repository
git clone https://github.com/your-repo/flask-api.git
cd flask-api
# Create a virtual environment and activate it
python3 -m venv venv
source venv/bin/activate
- Install the dependencies:
pip install -r requirements.txt
- Configure your
.env
file on EC2 with the database credentials for RDS:
echo "DB_HOST=<your-rds-endpoint>" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_NAME=<your-database-name>" >> .env
echo "DB_USER=<your-username>" >> .env
echo "DB_PASSWORD=<your-password>" >> .env
- Start the Flask app:
# Set Flask to listen on all interfaces and port 5000
flask run --host=0.0.0.0 --port=5000
- Test the app by going to your EC2 public IP and port 5000 in the browser or using
curl
:
curl http://<your-ec2-public-ip>:5000/health
To run Flask as a background service on EC2:
- Create a systemd service file for the Flask app:
sudo nano /etc/systemd/system/flaskapp.service
- Add the following content to the file:
[Unit]
Description=Flask Application
After=network.target
[Service]
User=ec2-user
WorkingDirectory=/home/ec2-user/flask-api
Environment="DB_HOST=<your-rds-endpoint>"
Environment="DB_PORT=5432"
Environment="DB_NAME=<your-database-name>"
Environment="DB_USER=<your-username>"
Environment="DB_PASSWORD=<your-password>"
ExecStart=/home/ec2-user/flask-api/venv/bin/python /home/ec2-user/flask-api/app.py
[Install]
WantedBy=multi-user.target
- Reload the systemd daemon, start the service, and enable it to start on boot:
sudo systemctl daemon-reload
sudo systemctl start flaskapp
sudo systemctl enable flaskapp
- Verify the service is running:
sudo systemctl status flaskapp
- GET /health: Check the health of the API and database connection.
- POST /items: Create a new item.
- GET /items/<item_id>: Retrieve an item by ID.
- PUT /items/<item_id>: Update an item by ID.
- DELETE /items/<item_id>: Delete an item by ID.
# Example curl request
curl http://<your-ec2-public-ip>:5000/health
Your API is now deployed and connected to an RDS PostgreSQL database on AWS.