Reputation: 277
I have a Flask app that I have deployed to AWS Elastic Beanstalk and all works well. However I currently have the SQLAlchemy connection string hard-coded as the live AWS string. I need now to set thing up so that the AWS server automatically picks up the correct string, and my local environment uses the dev (local string). I have been reading the following post on how to achieve this:
Flask: How to manage different environment databases?
This makes sense in theory but I can't find any practical examples of how to actually achieve this on AWS Elastic Beanstalk. My init.py code looks like this:
from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from flask_cors import CORS, cross_origin
from flask_sqlalchemy import SQLAlchemy
application = app = Flask(__name__)
app.config.from_object('config')
app.config.from_envvar('XXXXXPRODUCTION_CONFIG',silent=True)
db = SQLAlchemy(app)
CORS(app)
Config.py looks like this:
SQLALCHEMY_DATABASE_URI = 'mysql://root:xxxxxxx@localhost/xxxxx'
SQLALCHEMY_TRACK_MODIFICATIONS = False
DEBUG = True
And Config_production_dev.py looks like this:
SQLALCHEMY_DATABASE_URI = 'mysql://xxxxxx:xxxxxx@#########.cuhrbowyd8hk.eu-west-2.rds.amazonaws.com/ebdb'
SQLALCHEMY_TRACK_MODIFICATIONS = False
DEBUG = False
So everything is set up but I now have no idea how to initiate this part for the AWS instance:
app.config.from_envvar('XXXXXPRODUCTION_CONFIG',silent=True)
According to the post mentioned above I need something like this:
environment=XXXXXPRODUCTION_CONFIG="/home/tigra/mycoolapp/settings_production.py"
But is the path shown a path on my local machine? Or a path to my Elastic Beanstalk app? And if so how do I find the details of this path? Also how do I load this command up into the EB environment so that it executes? I can't find this information anywhere. Any help would be much appreciated.
I'm deploying the app using eb deploy in eb cli.
Upvotes: 0
Views: 109
Reputation: 277
I worked out the final step so please see below if you're stuck with the same. Elastic Beanstalk Environment Variables are the answer. In my case I needed to run the following command in eb cli:
eb setenv XXXXXPRODUCTION_CONFIG=/opt/python/current/app/config_production_dev.py
The path is to the config_production_dev.py file in my app folder on the Elastic Beanstalk server and I found it by navigating using SSH.
It's also possible to set Environment Variables via your browser here:
Login to AWS Then
services > elastic beanstalk > "your-env" > configuration > software configuration
Then scroll down to "Environment Properties". In my case:
Property Name : XXXXXPRODUCTION_CONFIG
Property Value : /opt/python/current/app/config_production_dev.py
Upvotes: 1