Problems using MySQL with AWS Lambda in Python

后端 未结 9 1108
南方客
南方客 2020-12-05 17:43

I am trying to get up and running with AWS Lambda Python (beginner in Python btw) but having some problems with including MySQL dependency. I am trying to follow the instruc

9条回答
  •  庸人自扰
    2020-12-05 18:29

    AWS recently came out with a great solution for the issue of database drivers and database access in Lambda: the Aurora Data API. The Data API tunnels SQL over HTTP using AWS standard auth. This bypasses the problems with compiling native code and using traditional database connection models in Lambda.

    I ended up writing a DB-API compatible driver for it: aurora-data-api (and a SQLAlchemy dialect using it):

    import aurora_data_api
    
    cluster_arn = "arn:aws:rds:us-east-1:123456789012:cluster:my-aurora-serverless-cluster"
    secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:MY_DB_CREDENTIALS"
    with aurora_data_api.connect(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn, database="my_db") as conn:
        with conn.cursor() as cursor:
            cursor.execute("select * from pg_catalog.pg_tables")
            print(cursor.fetchall())
    

提交回复
热议问题