Deploying Flask App with sqlite db in AWS Elastic Beanstalk - amazon-web-services

I have the tutorial app from the flask docs and modified for my use.
I was able to deploy my flask app and create the db via container_commands (flask init-db)
But when I try to write something to the db from the web browser. It throws the exception that
"sqlite3.OperationalError: attempt to write a readonly database"
It seems the problem is the write permission to the sqlite file. But it was created at the time of deploying the application. Any help.
Ideally when you are deploying in your own production env (not cloud), the sqlite is created in the venv/var/app-instance folder. How do I access this in AWS EB.

It looks like file write permission issue to me as well.
here is how i would troubleshoot the issue:
login to the instance via ssh, give permission to the file and see if it resolves the issue.
if it does resolves the issue, I would automate that using .ebextensions
Hope this helps you move forward.
Also look at the reference below shows how to change file permissions.
Reference: python sqlite3 OperationalError: attempt to write a readonly database

Related

Moodle on Heroku using S3 Bucket

I'm trying to set up Moodle on a free Dyno on Heroku.
I started my project with the stable version 3.10 of Moodle on a new repository.
I've created the Heroku app and connected my github repository to it
I've created an S3 Bucket that allows read and write access to objects from anyone
On config.php:
I changed the database settings (to point to the free postgres database that I also created),
I've set wwwroot as my Heroku app (http://myherokuappexample.herokuapp.com/)
I've set dataroot as my S3 Bucket endpoint (https://mys3bucketnamexample.s3.eu-north-1.amazonaws.com/moodledata/)
I created the folder moodledata, added an image to it and I was able to access it from the browser (for example https://mys3bucketnamexample.s3.eu-north-1.amazonaws.com/moodledata/ola.png)
BUT I'm getting "Fatal error: $CFG->dataroot is not configured properly, directory does not exist or is not accessible! Exiting." when the app is deployed to Heroku.
What am I doing wrong?

AWS Lightsail Django Bitnami : Operational Error- Attempt to write read only database

I have a website that is completely functional in development, however, I'm unable to configure the server permissions for my .sqlite3database file, so as to allow database changes eg Login, and Register. I'm using the bitnami stack for Django on AWS lightsail and I would appreciate help on the matter. Thanks !
Bitnami developer here!
Django stack uses Apache as its server, which is configured to delegate those requests to a Python daemon process.
It may be the case that if you were using a venv or extra python configuration, you'd have to adapt the virtual host configuration file to take that into account.
$ cat /opt/bitnami/apache2/conf/vhosts/myapp-http-vhost.conf
...
SGIDaemonProcess APPNAME python-home=/opt/bitnami/python python-path=/opt/bitnami/projects/APPNAME
...
Regards!

How to decouple local directory and Heroku project

I am developing an app with Django and I successfully pushed it on Heroku.
This app has a postgres database and a form to allow users to fill the database.
I have coupled the local directory and Heroku, so that both if I run the server by command prompt, or if I access the app, and submit the form, my database get changed.
Now I want to make some experiments on the local database without changing the one on Heroku.
Is it possible?
Can I do it by just commenting the database URL in settings.py ?
I have searched for this matter on Google but I don't know the name it, so that I cound not find proper answer.
This is not a question of "decoupling" directories. It is because you are trying to use sqlite on Heroku, and you have added the sqlite file to your git repo.
You cannot use sqlite on Heroku; use the postgres add-on. Additionally, your sqlite file must not be in source control.

How to download updated sqlite Database from AWS EB? (Django app)

I have deployed my django app on AWS Elastic beanstalk, using the sqlite database. The site has been running for a while.
Now I have made some changes to the website and want to deploy again, but by keeping the changes the database went through(by form inputs)
But I am not able to find a way to download the updated database or any files. The only version of the app I am able to download is the one I already uploaded when I first deployed.
How can I download the current running app version and deploy with the same database?
you may use scp to connect to remote server and download all your files.
as commented, you should better use dumpdata and then i/o that file (instead of the db or full site).
however, if your remote system python version is not the same as your django site, then the dumpdata may not work (this is my case), so I managed to download the whole site folder back to local and then use dumpdata
aws elastic beanstalk Python app's files in /opt/python/current/app
just use scp -r to download the whole site to local.
I believe you have sqlite db file being packaged from your local during eb deploy. If that is a case then all you need to do is not include db.sqlite file in your deployments.
Move location of db.sqllite file to s3 or ebs persistent volume so that it persists.
Use .ebextentions to run db migration in aws on top of the aws db file just the way django recommends.
To be on safe side you can ssh into your eb env which is ec2 instance and download db.sqlite file just incase.

Django Web application publishing

How to publish django web application? what are the steps to be followed for publishing a django application? is there any free hosting for pulishing?
thanks in advance
I am using PythonAnyWhere for hosting my project. They provide hosting for python-Django / flask specificly and have a very dedicated team.
You can host your app for free but you can not use your own domain. To use custom domain, you need to purchase paid plan starting from $5 per month.
Their customer support is extremly good.
Steps to follow for hosting:
- Make sure your code is production ready i.e. well tested and bug free.
- Separate production and dev settings file. On production you need to set DEBUG=false.
- Push your code to github. Do not commit sensitive information like credentials or api keys. Keep them in .env file.
- Create account on pythonanywhere server and create database.
- Clone the git repo. Complete the web setup step. Edit the wsgio settings file.
- Run migrations. python manage.py migrate.
- Create cache table if any python manage.py createcachetable.
- Collect static files python manage.py collectstatic.
- Reload the web app.
Refer this step by step article to setup a free account and host web app on pythonanywhere server.
I'm a big fan of Amazon Web Services if you want simplicity and automation. You can create a CodeStar project for Django and Elastic Beanstalk. Everything is set up from you, so as soon as you commit code to your github repo (it even can create the repo for you with the elastic beanstalk config files), it builds, tests, then deploys it.
The best part is you can utilize amazon's free-tier level if you are a new member so it's probably going to be free or extremely cheap.
Look here for reference:
https://aws.amazon.com/codestar/faqs/
digitalocean.com is good for hosting. link
I used this for Django 2.0 with python3.