# 1、 SQS——lambda import json import boto3 import time import pymysql def updata_db(m_key,m_value): conn2 = pymysql.connect( host='a.xdc.ink', port=3306, user='root', passwd='xclouds12', db='cmd', charset='utf8mb4') cs2 = conn2.cursor() select_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{m_key}", "{m_value}");' cs2.execute(select_sql) conn2.commit() cs2.close() conn2.close() def lambda_handler(event, context): # print(event) time.sleep(3) body = event['Records'][0]['body'] m_key = event['Records'][0]['messageAttributes']['mykey']['stringValue'] # print("FORMSQS:" + m_key + ":" + body) ddb= boto3.client('dynamodb') table_name = 'found' ddb.update_item( TableName=table_name, ExpressionAttributeValues={ ':y': { 'S': body, } }, Key={ 'm_key': { 'S': m_key, } }, UpdateExpression='SET sqs = :y', ReturnValues="UPDATED_NEW" ) res = ddb.get_item( Key={ 'm_key': { 'S': m_key, } }, TableName=table_name,) lambda_code=res['Item']['lambda']['S'] https_code=res['Item']['https']['S'] sqs_code=res['Item']['sqs']['S'] order=res['Item']['order']['S'] # print(order,https,lambdaa,sqs) if "012" == order: valueb = sqs_code+https_code+lambda_code valuea = valueb.replace("\n","") # print(valuea,keya) updata_db(m_key,valuea) elif "021" == order: valueb = sqs_code+lambda_code+https_code valuea = valueb.replace("\n","") updata_db(m_key,valuea) elif "102" == order: valueb = https_code+sqs_code+lambda_code valuea = valueb.replace("\n","") updata_db(m_key,valuea) elif "120" == order: valueb = https_code+lambda_code+sqs_code valuea = valueb.replace("\n","") updata_db(m_key,valuea) elif "201" == order: valueb = lambda_code+sqs_code+https_code valuea = valueb.replace("\n","") updata_db(m_key,valuea) elif "210" == order: valueb = lambda_code+https_code+sqs_code valuea = valueb.replace("\n","") updata_db(m_key,valuea) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } # 2、API-gateway lambda import json import boto3 import time def lambda_handler(event, context): time.sleep(2) # print(event) # print(event) body = json.loads(event['body']) # print(type(body)) val = body["Message"] key = body['MessageAttributes']['mykey']['Value'] print("FORMHTTPS:" + key + ":" + val) ddb= boto3.client('dynamodb') table_name = 'found' ddb.update_item( TableName=table_name, ExpressionAttributeValues={ ':y': { 'S': val, } }, Key={ 'm_key': { 'S': key, } }, UpdateExpression='SET https = :y', ReturnValues="UPDATED_NEW" ) # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } # 3、lambda - lambda import json import boto3 def lambda_handler(event, context): # TODO implement # print(event) sec = event['Records'][0]['Sns']['MessageAttributes'] mes = event['Records'][0]['Sns']['Message'] m_key = sec['mykey']['Value'] order = sec['order']['Value'] a1 = order.replace(":", "") a2 = a1.replace("SQS", '') a3 = a2.replace("HTTPS","") a4=a3.replace("LAMBDA","") a5=a4.replace(" ","") print("ORDER:"+ m_key + ":" + a5) print("FORMLAMBDA:" + m_key + ":" + mes) # updata_db(m_key,a5) ddb= boto3.client('dynamodb') tableName = 'found' item = { 'm_key': {'S': m_key}, 'order': {'S': a5}, 'lambda': {'S': mes} } ddb.put_item(TableName = tableName, Item = item)