AWS Lambda with python sqlite3

Viewed 3032

I am uploading a .zip file to AWS Lambda which contains a .py file. After uploading I am getting an error like this:

attempt to write a readonly database
[ERROR] OperationalError: table "AuData1" already exists
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 15, in lambda_handler
    cur.execute('''END RequestId: f5539447-a6d8-47ed-b415-5e2971923357

The code for .py file is:

def lambda_handler(event,context):
    try:
        conn = sqlite3.connect('SearchResultData.db')
        cur = conn.cursor()
        cur.execute('DROP TABLE IF EXISTS AuData1')
    except Exception as e:
        print(e)

    cur.execute('''
    CREATE TABLE "AuData1" (
.......

So basically the sqlite database present in the folder contains this table 'AuData1' but I want it to drop that and create a new one. However the database is not accessible as it says "readonly database". Please help if any solution available. Thank you in advance

1 Answers

With Lambda you can write files in the /tmp folder, but not in other locations. It sounds like you are including the sqllite DB in the Zip you are uploading to lambda, and if so, you won't be able to change it because of the folder it will be in.

You could first copy the SearchResultData.db file to /tmp if it doesn't already exist there. Then you can connect to the DB that is in the /tmp directory and write to it. But if you are expecting that database to be shared by other lambda invocations, don't count on that. If you need a persistent database, you should create one outside of lambda and connect to it. That said, this article says

"Each execution environment provides 512 MB of disk space in the /tmp directory. The directory content remains when the execution environment is frozen, providing a transient cache that can be used for multiple invocations. You can add extra code to check if the cache has the data that you stored. For more information on deployment size limits, see AWS Lambda quotas."

Check out this for ideas:

Related