简体   繁体   中英

How to abstract DB connection in AWS Lambda?

I'm building an application in AWS with quite a few Lambda functions to write - and all of them will create a database instance to query on, by running the following code:

mydb = mysql.connector.connect(
host="endpoint.rds.amazonaws.com",
user="user",
passwd="password",
database="dbname"
)

Now, I don't want to have to explicitly include this code in every single Lambda function - I'd rather put it somewhere else (either in a Layer or a separate Lambda function), so that this could be done simply by something like this:

mydb = ConnectToDB()

Any thoughts on how to do this?

Solved. I created a python file called DBConnections,py. which includes the function below - and I included it in the deployment package for my AWS Lambda Layer.

def Connect():

mydb = mysql.connector.connect(
host="endpoint.amazonaws.com",
user="user",
passwd="password",
database="mydbname"
)

return mydb

After it was deployed, the only thing I have to do to invoke this is:

from DBConnections import Connect
mydb = Connect()

Voilà.

You have the right idea, assuming you are using python i would create a layer package similar to the following:

python/
    myPackage.py
    mysql/

Where mysql includes the mysql package and myPackage.py includes some variation of:

import mysql

def ConnectToDB(**kwargs):
      return mysql.connector.connect(
                host=kwargs.get("YOUR_ENDPOINT"),
                user=kwargs.get("YOUR_USER"),
                passwd=kwargs.get("YOUR_PASSWORD"),
                database=kwargs.get("YOUR_DBNAME")
          )

Then use this script to create a layer in lambda:

#!/bin/bash

#Required variables
LAYER_NAME="YOUR_LAYER_NAME"
LAYER_DESCRIPTION="YOUR_LAYER_DESCRIPTION"
LAYER_RUNTIMES="python3.6 python3.7"
S3_BUCKET="YOUR_S3_BUCKET"

#Zip Package Files
zip -r ${LAYER_NAME}.zip .
echo "Zipped ${LAYER_NAME}"

#Upload Package to Lambda
aws s3 cp ./${LAYER_NAME}.zip s3://${S3_BUCKET}

#Create new layer
aws lambda publish-layer-version --layer-name ${LAYER_NAME} --description "${LAYER_DESCRIPTION}" --content S3Bucket=${S3_BUCKET},S3Key=${LAYER_NAME}.zip --compatible-runtimes ${LAYER_RUNTIMES}

#Cleanup zip files
rm ${LAYER_NAME}.zip

you can then associate the layer with your lambda function, and import your package in the lambda using the following syntax:

from myPackage import ConnectToDB

connectionParams = {
    "YOUR_ENDPOINT" : ...,
    "YOUR_USER": ...,
    "YOUR_PASSWORD": ...,
    "YOUR_DBNAME": ...
}
mydb = ConnectToDB(**connectionParams)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM