Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SQLAlchemy Database with AWS Elastic Beanstalk - waste of time?

I have successfully deployed a Flask application to AWS Elastic Beanstalk. The application uses an SQLAlchemy database, and I am using Flask-Security to handle login/registration, etc. I am using Flask-Migrate to handle database migrations.

The problem here is that whenever I use git aws.push it will push my local database to AWS and overwrite the live one. I guess what I'd like to do is only ever "pull" the live one from AWS EB, and only push in rare circumstances.

Will I be able to access the SQLAlchemy database which I have pushed to AWS? Or, is this not possible? Perhaps there is some combination of .gitignore and .elasticbeanstalk settings which could work?

I am using SQLite.

like image 838
b_g Avatar asked Dec 14 '22 15:12

b_g


2 Answers

Yes, your database needs to not be in version control, it should live on persistent storage (most likely the Elastic Block Storage service (EBS)), and you should handle schema changes (migrations) using something like Flask-Migrate.

The AWS help article on EBS should get you started, but at a high level, what you are going to do is:

  • Create an EBS volume
  • Attach the volume to a running instance
  • Mount the volume on the instance
  • Expose the volume to other instances using a Network File System (NFS)
  • Ensure that when new EBS instances launch, they mount the NFS

Alternatively, you can:

  • Wait until Elastic File System (EFS) is out of preview (or request access) and mount all of your EB-started instances on the EFS once EB supports EFS.
  • Switch to the Relational Database Service (RDS) (or run your own database server on EC2) and run an instance of (PostgreSQL|MySQL|Whatever you choose) locally for testing.
like image 117
Sean Vieira Avatar answered Jan 05 '23 15:01

Sean Vieira


The key is hosting your database outside of your Elastic Beanstalk environment. If not, as the load increases different instances of your Flask app will be writing to their own local DB. There won't a "master" database that will contain all the commits.

The easiest solution is using the AWS Relational Database Service (RDS) to host your DB as an outside service. A good tutorial that walks through this exact scenario:

Deploying a Flask Application on AWS using Elastic Beanstalk and RDS

SQLAlchemy/Flask/AWS is definitely not a waste of time! Good luck.

like image 30
manychairs Avatar answered Jan 05 '23 16:01

manychairs