For my Flask app, I want to use the Flask-SQLAlchemy extension to connect to a database instance I created on AWS RDS.
When I try to connect, the application times out and I get the following error:
sqlalchemy.exc.OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on 'xxxxxxxxxxxxxxx.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com'(60")
My Code Looks Like This:
from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy application = Flask(__name__) application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{master username}:{db password}@{endpoint}/{db instance name}' db = SQLAlchemy(application) @application.route('/') def hello_world(): return 'Hello World' if __name__ == '__main__': application.run()
The Flask-SQLAlchemy documentation says that the format of the SQLALCHEMY_DATABASE_URI for mysql database connections should look like this:
mysql://username:password@server/db
Additionally, I can check the AWS RDS console for information on my database instance. The console looks like this.
Right now I am assuming that "username" in SQLAlchemy refers to the "Master Username" in the AWS RDS console, "server" in SQLAlchemy refers to the "Endpoint" in the AWS RDS console, and "db" refers to the "Database Instance Name"
What am I doing wrong??
If someone could describe the SQLALCHEMY_DATABASE_URI for me with the AWS RDS console terms, that would completely solve the problem.
Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/ . In the navigation pane, choose Databases to display a list of your DB instances. Choose the name of the MySQL DB instance to display its details. On the Connectivity & security tab, copy the endpoint.
You then create a Flask application instance called app , which you use to configure two Flask-SQLAlchemy configuration keys: SQLALCHEMY_DATABASE_URI : The database URI to specify the database you want to establish a connection with. In this case, the URI follows the format sqlite:/// path/to/database. db .
To connect to a private RDS DB instance from a local machine using an EC2 instance as a jump server, follow these steps: Launch and configure your EC2 instance and configure the network setting of the instance. Configure the RDS DB instance's security groups. Connect to the RDS DB instance from your local machine.
Go to your AWS RDS console and choose your instance -> DB Security Groups -> Click Default
and add a new CIDR as per the recommended IP range.
This is basically a firewall configuration which determines who can connect to the database instance.
You could set it as 0.0.0.0/0 so that firewall doesn't prevent you from accessing it from any host/network.
This is a valid path: EC2 dashboard -> Find network & security from left side panel -> Security groups.
If you have a security group created, you may go to number 2.
There you can name your security group and description.
VPC will probably be automatically created for you.
After creating it, select it from the list.
Here you can configure inbound connections rules.
On edit -> add rule you have default presets for easy configuration, for example HTTP on port 80.
I hope this helps everyone from now on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With