-
Notifications
You must be signed in to change notification settings - Fork 3
Set up RDS Database for use by Elastic Beanstalk
Many Flask applications require access to a database in order to persist data. AWS provide their RDS offering. RDS supports many different types of databases and is also available on the Free tier if you select a small database.
For our purposes, a small MySQL v5.7x database is perfect.
There are a number of steps required to set-up a separate RDS database for use by our Flask app running on Elastic Beanstalk. These are:
- creation of a security group enabling access to the RDS database
- creation of the new RDS database
- installing the initial database tables
- configuring the Elastic Beanstalk environment to use this database.
We will complete these steps via the command line. The AWS CLI must be installed prior to executing these steps.
Ensure the AWS CLI v2 is installed and subsequently configured for use on your local machine as described in these AWS pages.
VPC Security Groups are used to control the ingress into the database i.e. which other security groups or IP addresses can access the database. For our purposes, we need to ensure the Security Group that our Elastic Beanstalk environment uses is allowed access as well as the IP address of our local machine. The latter is required to perform the initial configuration of the database.
The below command creates the necessary Security Group. It will output the GroupID Make a note of this GroupID as you require it later. This GroupID begins with sg-
aws ec2 create-security-group --group-name flask-db-access --description "Provides access to the database"
If you now enter the below command, replacing <security group id> with the sg-xxxx value output by the previous command then you see the full details of this new group
aws ec2 describe-security-groups --group-ids <security group id>
Now, we need to provide ingress access to our local machine and the EB Security Group. For this, you will require the IP address of your local machine and GroupID of the EB Security Group that your EB environment is using.
For your IP address, you can go to a site such as whatismyipaddress and it will show it.
For the EB Security Group then enter:
aws ec2 describe-security-groups
to see all of the security groups, including the one just created. Look for the one that has a description of "Security Group for ElasticBeanstalk Environment". Make a note of its GroupID. This will be of the form sg-xxx.
Now you have the IP address and the ElasticBeanstalk security group, enter the following two commands ensuring to replace <SECURITY GROUP ID> with the GroupID you created earlier, <YOUR IP ADDRESS> with your machine's IP address and <EB SECURITY GROUPID> with the GroupID you just viewed.
aws ec2 authorize-security-group-ingress --group-id <SECURITY GROUP ID> --protocol tcp --port 3306 --cidr <YOUR IP ADDRESS>
aws ec2 authorize-security-group-ingress --group-id <SECURITY GROUP ID> --protocol tcp --port 3306 --source-group <EB SECURITY GROUPID>
Now we have the security group created, we can create the RDS database. We will create a small MySQL database server complete with a new database that our Flask application can use.
The following command will create the database. Replace with the sg-xxx identifier of the new security group that you created earlier.
This database is marked as publicly-accessible so we can access it from our development machine.
aws rds create-db-instance \
--db-name flaskdev \
--db-instance-identifier flask-db \
--allocated-storage 20 \
--db-instance-class db.t3.small \
--engine mysql \
--master-username flask \
--master-user-password helloflask \
--vpc-security-group-ids <SECURITY GROUP ID> \
--engine-version 5.7.31 \
--publicly-accessible
You can execute the command
aws rds-describe-db-instances
to see further information about the new database environment and database. It takes a while to initialise. You can see the Status progress if you execute this command repeatedly for a few minutes or so.
Make a note of the Address value within the Endpoint section near the top of the output JSON. This is the address you will need in order to connect to the database. It should be of the form flask-db.xxxx.us-west-1.rds.amazonaws.com where xxx is some numeric string and the region hosting the database is also shown. This region will be different if you're not running it in us-west-1.
An Elastic Beanstalk environment variable is used to store the database connection string.
On your local machine, enter the below replacing <DATABASE ADDRESS> with your database address as noted previously.
eb setenv DEV_DATABASE_URL='mysql+pymysql://flask:helloflask@<DATABASE ADDRESS>/flaskdev'
it will take a few minutes to update the environment. You should see a "Successfully deployed new configuration to environment" message on completion.
Now check the status of the environment. It should show a Health value of green
eb status
Let's look at the website and check it is working
eb open
You should see the home screen 😄