Sfoglia il codice sorgente

add sqs_https_lambda.py.

xdc 5 anni fa
parent
commit
67a0734af8
1 ha cambiato i file con 160 aggiunte e 0 eliminazioni
  1. 160 0
      sqs_https_lambda.py

+ 160 - 0
sqs_https_lambda.py

@@ -0,0 +1,160 @@
+
+# 1、 SQS——lambda
+import json
+import boto3
+import time
+import pymysql
+
+def updata_db(m_key,m_value):
+    conn2 = pymysql.connect(
+        host='XXXxxx',
+        port=3306,
+        user='root',
+        passwd='1234',
+        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']
+
+    
+    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']
+
+    if "012" == order:
+        valueb = sqs_code+https_code+lambda_code
+        valuea = valueb.replace("\n","")
+        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)
+    body = json.loads(event['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"
+    )
+    return {
+        'statusCode': 200,
+        'body': json.dumps('Hello from Lambda!')
+    }
+
+# 3、lambda - lambda
+
+import json
+import boto3
+
+def lambda_handler(event, context):
+    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)
+    
+    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)
+