import json import pymysql def insert_data(mykey,myvalue): # 打开数据库连接 db = pymysql.connect("myinstance.cfeisxz72aaf.rds.cn-northwest-1.amazonaws.com.cn", "cstor", "12345678", "mydb") # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 插入语句 sql = "INSERT INTO kv(mykey, myvalue) VALUES ('%s', '%s')" % (mykey, myvalue) try: # 执行sql语句 cursor.execute(sql) # 执行sql语句 db.commit() except: # 发生错误时回滚 db.rollback() # 关闭数据库连接 db.close() def handle_order(s): arr = s.split(" ") dict = {} for i in arr: temp = i.split(":") if temp[1] == "0": dict["0"] = temp[0] elif temp[1] == "1": dict["1"] = temp[0] elif temp[1] == "2": dict["2"] = temp[0] #根据key 排序 a = sorted(dict.items(),key= lambda d :d[0]) return a def lambda_handler(event, context): # TODO implement for rec in event["Records"]: # TODO: write code... if rec["eventName"] != "MODIFY": continue new_image = rec["dynamodb"]["NewImage"] #获取 key value sqs = new_image.get("SQS") https = new_image.get("HTTPS") lmd = new_image.get("LAMBDA") order = new_image.get("order") mykey = new_image.get("mykey").get("S") #不是最后一次更新 if sqs==None or https==None or lmd==None or order==None: continue #更新一下值,上面的值不全的话,可能会是None,就不能直接get sqs = sqs.get("S") https = https.get("S") lmd = lmd.get("S") order = order.get("S") #处理order arr = handle_order(order) # arr = [('0', 'HTTPS'), ('1', 'SQS'), ('2', 'LAMBDA')] #拼接字符串 myvalue = "" for item in arr: # TODO: write code... if item[1]=="SQS": myvalue += sqs elif item[1] == "HTTPS": myvalue += https elif item[1] == "LAMBDA": myvalue += lmd print(mykey,myvalue) #插入数据库 insert_data(mykey,myvalue) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } # 2、sqs import json import boto3 table_name = "mydynamodb" zujian = "SQS" def insert_data(mykey,order,msg): client = boto3.client('dynamodb') response = client.get_item(TableName=table_name,Key={"mykey":{"S":mykey}}) item = response.get("Item") print(item) response = client.update_item( TableName=table_name, Key={"mykey":{"S":mykey}}, AttributeUpdates={ "order":{"Value":{"S":order}}, zujian:{"Value":{"S":msg}} }) def lambda_handler(event, context): # TODO implement for rec in event["Records"]: body = rec["body"] body = json.loads(body) msg = body["Message"] attr = body["MessageAttributes"] #获取 mykey order mykey = attr["mykey"]["Value"] order = attr["order"]["Value"] print(msg) print(mykey,order) #将 mykey order msg 插入数据库 insert_data(mykey,order,msg) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } # 3、 Lambda import json import boto3 table_name = "mydynamodb" zujian = "LAMBDA" def insert_data(mykey,order,msg): client = boto3.client('dynamodb') response = client.get_item(TableName=table_name,Key={"mykey":{"S":mykey}}) item = response.get("Item") print(item) response = client.update_item( TableName=table_name, Key={"mykey":{"S":mykey}}, AttributeUpdates={ "order":{"Value":{"S":order}}, zujian:{"Value":{"S":msg}} }) def lambda_handler(event, context): # TODO implement for rec in event["Records"]: sns = rec["Sns"] subject = sns["Subject"] msg = sns["Message"] attr = sns["MessageAttributes"] #获取 mykey order mykey = attr["mykey"]["Value"] order = attr["order"]["Value"] print(msg) print(mykey,order) #将 mykey order msg 插入数据库 insert_data(mykey,order,msg) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } # 4、 https import json import boto3 table_name = "mydynamodb" zujian = "HTTPS" def insert_data(mykey,order,msg): client = boto3.client('dynamodb') response = client.get_item(TableName=table_name,Key={"mykey":{"S":mykey}}) item = response.get("Item") print(item) response = client.update_item( TableName=table_name, Key={"mykey":{"S":mykey}}, AttributeUpdates={ "order":{"Value":{"S":order}}, zujian:{"Value":{"S":msg}} }) def lambda_handler(event, context): # TODO implement if event["httpMethod"] == "POST": body = json.loads(event["body"]) msg = body["Message"] attr = body["MessageAttributes"] #获取 mykey order mykey = attr["mykey"]["Value"] order = attr["order"]["Value"] print(msg) print(mykey,order) #将 mykey order msg 插入 dynamodb insert_data(mykey,order,msg) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }