-
Notifications
You must be signed in to change notification settings - Fork 13
Description
RDS and Flask Setup
Tanisha Patil
1. Create a Security Group
Step 1: Login to AWS Management Console
- Go to the AWS Management Console.
- Log in with AWS credentials.
Step 2: Navigate to EC2 Security Groups
- In the AWS Management Console, navigate to the EC2 Dashboard.
- In the left-hand menu, scroll down and select Security Groups under Network & Security.
Step 3: Create a New Security Group
- Click on the Create security group button.
- Enter a name for security group. Name it something that reflects purpose ex. 'rds_access' or
RDS_Security_Group. - Select the VPC : vpc-faea2491 (tester)
This is the screen you should see

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 :
- Click on the Inbound rules tab.
- 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 :
- Click on the Outbound rules tab.
- Click on Add Rule
- Add outbound rule that allows all traffic (or restrict as per your requirement).
Step 6: Review and Create
- Review security group settings.
- Click on the Create security group button to finalize.
2. Create an RDS Instance
Step 1: Navigate to RDS Dashboard
- In the AWS Management Console, navigate to the RDS Dashboard by selecting RDS from the services menu.
Step 2: Launch DB Instance
- Click on the Create database button.
- Select the Standard Create option.
Step 3: Choose a Database Engine
- Select the database engine you want to use : MySQL
Step 4: Configure Database Settings
- Specify the template as Free teir
- DB instance identifier, Master username, and Master password.
NOTE : SAVE THE USERNAME AND PASSWORD INFORMATION FOR A LATER STEP
- Choose the DB instance size : db.t3.micro
Step 5: Configure Storage
- Choose the allocated storage size for your database.
- Enable storage auto-scaling if needed.
Step 6: Configure Connectivity
- In the Connectivity section, choose the VPC default.
- Select the Subnet group default
- Under Public access, choose YES.
- For VPC security group, choose the security group you created earlier (
RDS_Security_Group).
Step 7: Authentication Configuration
- Configure database authentication as password only
Step 8: Review and Launch
- Review all your settings.
- 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
- In the RDS Dashboard, click on your database instance to view its details.
- Copy the Endpoint and Port information.
4. Set up MySQL
Step 1. Connect to RDS
- 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
- Create Database Schema :
CREATE DATABASE yourdatabase; USE yourdatabase;
5. Configuring Flask
-
Navigate to
__init__.pyfile in flask_portfolio cloned in step 1 -
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}' -
You may have to add the following line to the requirements.txt file :
pymysql -
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'