AWS Lambda - Database writes not reflected by subsequent read

44 Views Asked by At

I have two lambda functions, one that inserts data into a database (create_user), and another (get_users) that reads it from a MySQL database hosted on AWS RDS.

The database code for the create_user lambda looks like this (note the commit):

user = ... # get user from request object

with self.conn.cursor() as cursor:
    cursor.execute(sql.INSERT_USER, (
        user.first_name,
        user.last_name,
        user.birth_date,
        user.sex,
        user.firebase_id
    ))
    self.conn.commit()

And the database code for the get_users lambda:

with self.conn.cursor() as cursor:
    cursor.execute(sql.GET_ALL_USERS)
    user_rows = cursor.fetchall()

In both lambdas I set up my database connection outside the handler function:

conn_params = db_utils.db_connection_parameters()

conn = pymysql.connect(host=conn_params['host'],
                      user=conn_params['username'],
                      password=conn_params['password'],
                      database=conn_params['name'],
                      cursorclass=pymysql.cursors.DictCursor)

def lambda_handler(event, context):
    ...

The issue is that the get_users lambda response does not contain users that were recently inserted with the create_user lambda or via MySQLWorkbench (again, remembering to call commit). If I connect to the database with pymysql or MySQLWorkbench, I can see that the new row was inserted, but the get_users lambda does not reflect this change for another 10 minutes or so. I'd like the results to be reflected nearly immediately.

1

There are 1 best solutions below

2
jerney On

Moving the connection from outside of the lambda handler function to within it fixed the issue:

conn_params = db_utils.db_connection_parameters()

def lambda_handler(event, context):
    user_id = int(event['pathParameters']['id'])

    conn = pymysql.connect(host=conn_params['host'],
                      user=conn_params['username'],
                      password=conn_params['password'],
                      database=conn_params['name'],
                      cursorclass=pymysql.cursors.DictCursor)

I don't understand why. I must be misunderstanding something about the way database connections work and what they cache.