nighthawkcoders / flask_2025

This project is a web application for managing Classroom instruction and operations. It is built using Flask, SQLAlchemy, and other requirements.txt dependencies.
https://flask2025.nighthawkcodingsociety.com/
0 stars 6 forks source link

RDS and Flask Setup #7

Open tanishapatil1234 opened 2 months ago

tanishapatil1234 commented 2 months ago

RDS and Flask Setup

Tanisha Patil

1. Create a Security Group

Step 1: Login to AWS Management Console

  1. Go to the AWS Management Console.
  2. Log in with AWS credentials.

Step 2: Navigate to EC2 Security Groups

  1. In the AWS Management Console, navigate to the EC2 Dashboard.
  2. In the left-hand menu, scroll down and select Security Groups under Network & Security.

Step 3: Create a New Security Group

  1. Click on the Create security group button.
  2. Enter a name for security group. Name it something that reflects purpose ex. 'rds_access' or RDS_Security_Group.
  3. Select the VPC : vpc-faea2491 (tester)

This is the screen you should see Create Security Group

Step 4: Configure Inbound and Outbound Rules

Note : Apply the rules that match YOUR requirements

Inbound :

  1. Click on the Inbound rules tab.
  2. Click on Add Rule to add the necessary inbound rules for your RDS instance:
    • Type: Custom TCP
    • Protocol: TCP
    • Port range: 3306
    • Source Type: Custom
    • Source : Specify the IP range that will be allowed to connect.

OR

Outbound :

  1. Click on the Outbound rules tab.
  2. Click on Add Rule
  3. Add outbound rule that allows all traffic (or restrict as per your requirement).

Step 6: Review and Create

  1. Review security group settings.
  2. Click on the Create security group button to finalize.

2. Create an RDS Instance

Step 1: Navigate to RDS Dashboard

  1. In the AWS Management Console, navigate to the RDS Dashboard by selecting RDS from the services menu.

Step 2: Launch DB Instance

  1. Click on the Create database button.
  2. Select the Standard Create option. image

Step 3: Choose a Database Engine

  1. Select the database engine you want to use : MySQL image

Step 4: Configure Database Settings

  1. Specify the template as Free teir

    image
  2. DB instance identifier, Master username, and Master password. NOTE : SAVE THE USERNAME AND PASSWORD INFORMATION FOR A LATER STEP

    image
  3. Choose the DB instance size : db.t3.micro

    image

Step 5: Configure Storage

  1. Choose the allocated storage size for your database.
  2. Enable storage auto-scaling if needed. image

Step 6: Configure Connectivity

  1. In the Connectivity section, choose the VPC default.
  2. Select the Subnet group default
  3. Under Public access, choose YES.
  4. For VPC security group, choose the security group you created earlier (RDS_Security_Group). image

Step 7: Authentication Configuration

  1. Configure database authentication as password only image

Step 8: Review and Launch

  1. Review all your settings.
  2. Click on the Create database button to launch your RDS instance.

Once your RDS instance is created, it will appear in the RDS dashboard, and you can connect to it using the endpoint provided.

3. Connect to Your RDS Instance

Step 1: Obtain the Endpoint

  1. In the RDS Dashboard, click on your database instance to view its details.

    image
  2. Copy the Endpoint and Port information.

    image

4. Set up MySQL

Step 1. Connect to RDS

  1. Use SQL client or use terminal to connect to RDS instance. You will be asked for the password set earlier for authentication. mysql -h your-rds-endpoint -P 3306 -u your-master-username -p

    image
  2. Create Database Schema :
    CREATE DATABASE yourdatabase; USE yourdatabase;

5. Configuring Flask

  1. Navigate to __init__.py file in flask_portfolio cloned in step 1

  2. Insert the following configuration (for RDS use in production environment) :

    DB_USERNAME = 'example_admin' DB_PASSWORD = 'example_password' DB_HOST = 'this_is_the_endpoint' DB_PORT = '3306' DB_NAME = 'example_db_name' dbURI = f'mysql+pymysql://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}'

  3. You may have to add the following line to the requirements.txt file : pymysql

  4. Run : pip install -r requirements.txt

Done, run python main.py ! Now the flask application will be accessing RDS. Optionally, configure __init__.py so that RDS is accessed only in production environment :

is_production = os.getenv('FLASK_ENV') == 'production'
if is_production:
    # Production - Use MySQL
    DB_USERNAME = 'example_admin'
    DB_PASSWORD = 'example_password'
    DB_HOST = 'this_is_the_endpoint'
    DB_PORT = '3306'
    DB_NAME = 'example_db_name'
    dbURI = f'mysql+pymysql://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}'
else:
    # Development - Use SQLite
    dbURI = 'sqlite:///volumes/sqlite.db'
tanishapatil1234 commented 2 months ago

🚧 Currently Researching 🚧

EXPORT :

Tool allows for export as JSON to specified path

image
jm1021 commented 1 month ago

This document is valuable and needs to be in KASM series of documents.