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)