Skip to content

RDS and Flask Setup  #7

@tanishapatil1234

Description

@tanishapatil1234

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 Rules: These rules define the incoming traffic allowed to reach your AWS resource. ex. if you have a web server, you might allow inbound HTTP and HTTPS traffic from the internet.
  • Outbound Rules: These rules specify the outgoing traffic allowed from your AWS resource to other destinations. ex. if your web server needs to access an external API, you would set up outbound rules to permit this traffic.

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

  • Type: Custom TCP
  • Protocol: TCP
  • Port range: 3306
  • Source Type: My IP

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
  1. DB instance identifier, Master username, and Master password.
    NOTE : SAVE THE USERNAME AND PASSWORD INFORMATION FOR A LATER STEP
image
  1. 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
  1. 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
  1. 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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions