A typical chalice app has several routes defined, but how do I properly connect that to an RDS so that I can save POST data, or retrieve data from a db? I already created a database through the RDS panel; I'm just not sure how to access it from Chalice.
Is it simply just having each route connect to the given RDS host with the user/pass?
For example,
from chalice import Chalice
import psycopg2
app = Chalice(app_name='test')
db_user = 'test'
db_pass = 'password'
db_host = 'https://.....'
db_port = 5432
@app.route('/save_data')
def save_data():
with psycopg2.connect(user=db_user, password=db_pass, ...) as conn:
with conn.cursor() as cur:
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (1,2))
@app.route('/get_data')
def get_data():
with psycopg2.connect(user=db_user, password=db_pass, ...) as conn:
with conn.cursor() as cur:
cur.execute("SELECT * FROM test")
@app.route('/something')
def something():
with psycopg2.connect(user=db_user, password=db_pass, ...) as conn:
with conn.cursor() as cur:
cur.execute(....) # some other query
If so, is there an easier way to handle this instead of having to specify the db connection each time? Are there weird session/race conditions associated with doing it this way?
Or, if I'm completely off, what's the proper way to connect to the RDS from Chalice routes?