3 Steps to create an AWS Database connection for MySQL Workbench and Flask-SQLAlchemy
Part1 _ AWS
First things first, sign up to AWS.
Before creating the Database, we need to ensure that we’re creating it under the most appropriate region, in our case, the closest one to home is the eu-west-2, which corresponds to London.
After logging in, navigate to the AWS console and the Amazon RDS product, from where we can create a Cloud Database to host our solution.
After that we navigate to the RDS dashboard and enter the all the creation parameters of the Database. In order to be able to access the database from home, I needed to add it to a security group, which allows the MariaDB port to connect from my home IP address:
Confirming the creation of the database kicks off the database creation. After a few minutes the database becomes available and we can now use it to continue our development.
After clicking on your DB name, you will see under Connectivity & Security that AWS then gives us the end point that we can use to connect to our new database.
Part 2_Connecting from MySQL Workbench
To connect from MySQL Workbench
- Download and install MySQL Workbench at Download MySQL Workbench.
- Open MySQL Workbench.
3. From Database, choose Manage Connections.
4. In the Manage Server Connections window, choose New.
5. In the Connect to Database window, enter the following information:
- Stored Connection — Enter a name for the connection, such as
- Hostname — Enter the DB instance endpoint.
- Port — Enter the port used by the DB instance.
- Username — Enter the username of a valid database user, such as the master user.
- Password — Optionally, choose Store in Vault and then enter and save the password for the user.
The window looks similar to the following:
You can use the features of MySQL Workbench to customize connections. For example, you can use the SSL tab to configure SSL connections. For information about using MySQL Workbench, see the MySQL Workbench documentation.
6. Optionally, choose Test Connection to confirm that the connection to the DB instance is successful.
7. Choose Close.
8. From Database, choose Connect to Database.
9. From Stored Connection, choose your connection.
10. Choose OK.
Part 3 _ Connect Flask app to AWS server
In the flask app, add the AWS DB end point to the connection string as below:
Then in the terminal, open Python and run the following commands in order:
- from app import db
Et Voila! You have successfully connected your flask App with your AWS server and you can verify the tables you have created in MySQL Workbench.