#1 https import boto3 import json table_name = 'hehe' def updata_ddb(mykey,order,msg): client = boto3.client('dynamodb') # response = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) # item = response.get("Item") # # if item == None and item.get('https'): client.update_item(TableName=table_name,Key={'mykey': {'S':mykey}},UpdateExpression='SET https = :https, orderss = :orderss', ExpressionAttributeValues={ ':https': {'S': msg}, ':orderss':{'S': order},}) # else: # pass def lambda_handler(event, context): message = json.loads(event['body'])['Message'] mykey = json.loads(event['body'])['MessageAttributes']['mykey']['Value'] order_orgain = json.loads(event['body'])['MessageAttributes']['order']['Value'] order = f"{order_orgain.split(' ')[0][-1]}{order_orgain.split(' ')[1][-1]}{order_orgain.split(' ')[2][-1]}" print(mykey,order,message) updata_ddb(mykey=mykey,order=order,msg=message) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } # SQS import boto3 import json import time import pymysql client = boto3.client('dynamodb') table_name = 'hehe' def updata_ddb(mykey,msg): client = boto3.client('dynamodb') client.update_item(TableName='hehe', Key={'mykey': {'S':mykey}}, UpdateExpression='SET sqs = :sqs', ExpressionAttributeValues={ ':sqs': { 'S': msg}, }) def exec_sql(mykey,myvalue): conn2 = pymysql.connect( host='aurora-db.cluster-chxg6aynimx1.rds.cn-northwest-1.amazonaws.com.cn', port=3306, user='admin', passwd='12345678', db='cmd', charset='utf8mb4') cs2 = conn2.cursor() ex_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{mykey}", "{myvalue}");' cs2.execute(ex_sql) conn2.commit() cs2.close() conn2.close() def sorta(num,sqs,https,lambdaa): if num == '012': return(f"{sqs}{https}{lambdaa}") elif num == '021': return(f"{sqs}{lambdaa}{https}") elif num == '102': return(f"{https}{sqs}{lambdaa}") elif num == '120': return(f"{lambdaa}{sqs}{https}") elif num == '201': return(f"{https}{lambdaa}{sqs}") elif num == '210': return(f"{lambdaa}{https}{sqs}") def lambda_handler(event,context): message = json.loads(event['Records'][0]['body'])['Message'] mykey = json.loads(event['Records'][0]['body'])['MessageAttributes']['mykey']['Value'] updata_ddb(mykey=mykey,msg=message) # time.sleep(1) try: res = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) sqs_code = message https_code = res['Item']['https']['S'] order_code = res['Item']['orderss']['S'] lambda_code = res['Item']['lambda']['S'] myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) exec_sql(mykey=mykey,myvalue=myvalue) except: time.sleep(1) res2 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) sqs_code = message https_code = res2['Item']['https']['S'] order_code = res2['Item']['orderss']['S'] lambda_code = res2['Item']['lambda']['S'] myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) exec_sql(mykey=mykey,myvalue=myvalue) finally: time.sleep(2) res3 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) sqs_code = message https_code = res3['Item']['https']['S'] order_code = res3['Item']['orderss']['S'] lambda_code = res3['Item']['lambda']['S'] myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) exec_sql(mykey=mykey,myvalue=myvalue) # 3. SNS import boto3 import json import time import pymysql client = boto3.client('dynamodb') table_name = 'hehe' def updata_ddb(mykey,msg): client = boto3.client('dynamodb') client.update_item(TableName='hehe', Key={'mykey': {'S':mykey}}, UpdateExpression='SET lambda = :lambda', ExpressionAttributeValues={ ':lambda': { 'S': msg}, }) def exec_sql(mykey,myvalue): conn2 = pymysql.connect( host='aurora-db.cluster-chxg6aynimx1.rds.cn-northwest-1.amazonaws.com.cn', port=3306, user='admin', passwd='12345678', db='cmd', charset='utf8mb4') cs2 = conn2.cursor() ex_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{mykey}", "{myvalue}");' cs2.execute(ex_sql) conn2.commit() cs2.close() conn2.close() def sorta(num,sqs,https,lambdaa): if num == '012': return(f"{sqs}{https}{lambdaa}") elif num == '021': return(f"{sqs}{lambdaa}{https}") elif num == '102': return(f"{https}{sqs}{lambdaa}") elif num == '120': return(f"{lambdaa}{sqs}{https}") elif num == '201': return(f"{https}{lambdaa}{sqs}") elif num == '210': return(f"{lambdaa}{https}{sqs}") def lambda_handler(event,context): mykey = event['Records'][0]['Sns']['MessageAttributes']['mykey']['Value'] message = event['Records'][0]['Sns']['Message'] updata_ddb(mykey=mykey,msg=message) # time.sleep(2) # try: # res = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) # sqs_code = res['Item']['sqs']['S'] # https_code = res['Item']['https']['S'] # order_code = res['Item']['orderss']['S'] # lambda_code = res['Item']['lambda']['S'] # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) # exec_sql(mykey=mykey,myvalue=myvalue) # except: # time.sleep(4) # res2 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) # sqs_code = res2['Item']['sqs']['S'] # https_code = res2['Item']['https']['S'] # order_code = res2['Item']['orderss']['S'] # lambda_code = res2['Item']['lambda']['S'] # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) # exec_sql(mykey=mykey,myvalue=myvalue) # finally: # time.sleep(5) # res3 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) # sqs_code = res3['Item']['sqs']['S'] # https_code = res3['Item']['https']['S'] # order_code = res3['Item']['orderss']['S'] # lambda_code = res3['Item']['lambda']['S'] # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) # exec_sql(mykey=mykey,myvalue=myvalue) # if sqs_code is not None and https_code is not None and order_code is not None and lambda_code is not None: # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) # exec_sql(mykey=mykey,myvalue=myvalue) # else: # time.sleep(1) # res4 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}}) # sqs_code = res4['Item']['sqs']['S'] # https_code = res4['Item']['https']['S'] # order_code = res4['Item']['orderss']['S'] # lambda_code = res4['Item']['lambda']['S'] # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code) # exec_sql(mykey=mykey,myvalue=myvalue) # return { # 'statusCode': 200, # 'body': json.dumps('Hello from Lambda!') # }