I need to deploy a lambda function carrying out a process with the python package pymysql, my python version is 3.6 and for this purpose I will use the lambda layers to avoid bring my whole code. In order to achieve this I have follow the steps below:
create a separate folder where I move the pymysql package installed in my virtual env cp -r /myvirtualenv/lib/python3.6/site-packages/pymysql ~/home/user/packagelambda/
within the path ~/home/user/packagelambda/
compress this package into a zip file to be uploaded as AWS lambda layer zip -r rdsconnection.zip *
upload this zipfile as new layer into aws lambda dashboard
Once I complete the process above , I went to my code and create two scripts to modularize my execution , a first script called md_conenction.py as below:
import json
import boto3
import pymysql
import csv
from datetime import datetime
def json_param():
s3 = boto3.resource('s3')
bucketname= "bucket"
file= "file.json"
object= s3.Object(bucketname,file)
body = object.get()['Body'].read().decode('utf8')
param= json.loads(body)
return param["host"], param["user"], param["password"], param["database"]
def db_connection():
host,user,password,database = json_param()
dbconnection = pymysql.connect(host,user,password,database)
dbcursor = dbconnection.cursor()
return dbconnection, dbcursor
Additionally a secon script in lambda functions by default called lambda_function.py with the content below:
import json
from md_connection import db_connection
def lambda_handler(event, context):
dbconnection, dbcursor = db_connection()
print(dbcursor)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Finally I when to my layers option on the designer and appends the new layer built previously. Once complete this process I still keep on getting the error Unable to import module 'lambda_function': No module named 'pymysql'
after every execution.
I would like to highlight that I have tried to carry out the process building a layer in two ways:
pymysql
pymysql
and PyMySQL-0.9.3.dist-info
Create a directory python
copy pymysql
and its dependency into the directory. Zip the python
directory and upload it to the layer. The folder structure inside the zip matters. The root directory should be python