我需要部署一个lambda函数,以python软件包pymysql来执行一个过程,我的python版本是3.6,为此,我将使用lambda层来避免带走我的整个代码。为了实现这一点,我遵循以下步骤:
创建一个单独的文件夹,在其中我将安装在虚拟环境{
"error": false,
"data": {
"id": "149",
"username": "kamal kumar",
"email": "kamalbhera96@gmail.com",
"phone_number": "9057217361",
"address": "B-209, Rajendra Marg, Bapu Nagar, Jaipur, Rajasthan 302015, India",
"city": "Jaipur",
"images": [
"http://moodsmatch.com/admin/uploads/20191209_151815.jpg",
"http://moodsmatch.com/admin/uploads/29261010_1348876641925767_1868739767978426368_n.jpg",
"http://moodsmatch.com/admin/uploads/20191209_112909.jpg",
"http://moodsmatch.com/admin/uploads/IMG_2ljalw.jpg",
"http://moodsmatch.com/admin/uploads/20191209_150617.jpg",
"http://moodsmatch.com/admin/uploads/FB_IMG_1575778091267.jpg"
],
"state": "Rajasthan",
"country": "India",
"referral_code": "Fnt4oe7C",
"referral_points": "0",
"membership_type": "Free",
"intersted_in": "",
"distance_in": "",
"defaultDistance": "5M",
"image": "http://www.moodsmatch.com/admin/../uploads/",
"gender": "Male",
"age": 0,
"dob": "12/02/2019",
"education": "he is a very good person ",
"hobby": "",
"about": "he is a very good person "
},
"message": "Record match"
}
在路径cp -r /myvirtualenv/lib/python3.6/site-packages/pymysql ~/home/user/packagelambda/
中将此包压缩为一个zip文件,以作为AWS lambda层~/home/user/packagelambda/
将此zipfile作为新层上传到AWS Lambda仪表板
完成上述过程后,我进入代码并创建了两个脚本来模块化执行,第一个脚本称为 md_conenction.py ,如下所示:
zip -r rdsconnection.zip *
另外,默认情况下,lambda函数中的secon脚本名为 lambda_function.py ,其内容如下:
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
最后,我何时进入设计器上的图层选项,并附加先前构建的新图层。完成此过程后,每次执行后我仍然继续收到错误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!')
}
。
我想强调一点,我试图通过两种方式进行构建图层的过程:
Unable to import module 'lambda_function': No module named 'pymysql'
下载的软件包pymysql
和pymysql
包括两个文件夹答案 0 :(得分:1)
创建目录python
,将pymysql
及其依赖项复制到目录中。压缩python
目录并将其上传到图层。拉链内的文件夹结构很重要。根目录应为python