sqs_https_lambda.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # 1、 SQS——lambda
  2. import json
  3. import boto3
  4. import time
  5. import pymysql
  6. def updata_db(m_key,m_value):
  7. conn2 = pymysql.connect(
  8. host='XXXxxx',
  9. port=3306,
  10. user='root',
  11. passwd='1234',
  12. db='cmd',
  13. charset='utf8mb4')
  14. cs2 = conn2.cursor()
  15. select_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{m_key}", "{m_value}");'
  16. cs2.execute(select_sql)
  17. conn2.commit()
  18. cs2.close()
  19. conn2.close()
  20. def lambda_handler(event, context):
  21. # print(event)
  22. time.sleep(3)
  23. body = event['Records'][0]['body']
  24. m_key = event['Records'][0]['messageAttributes']['mykey']['stringValue']
  25. ddb= boto3.client('dynamodb')
  26. table_name = 'found'
  27. ddb.update_item(
  28. TableName=table_name,
  29. ExpressionAttributeValues={
  30. ':y': {
  31. 'S': body,
  32. }
  33. },
  34. Key={
  35. 'm_key': {
  36. 'S': m_key,
  37. }
  38. },
  39. UpdateExpression='SET sqs = :y',
  40. ReturnValues="UPDATED_NEW"
  41. )
  42. res = ddb.get_item(
  43. Key={
  44. 'm_key': {
  45. 'S': m_key,
  46. }
  47. },
  48. TableName=table_name,)
  49. lambda_code=res['Item']['lambda']['S']
  50. https_code=res['Item']['https']['S']
  51. sqs_code=res['Item']['sqs']['S']
  52. order=res['Item']['order']['S']
  53. if "012" == order:
  54. valueb = sqs_code+https_code+lambda_code
  55. valuea = valueb.replace("\n","")
  56. updata_db(m_key,valuea)
  57. elif "021" == order:
  58. valueb = sqs_code+lambda_code+https_code
  59. valuea = valueb.replace("\n","")
  60. updata_db(m_key,valuea)
  61. elif "102" == order:
  62. valueb = https_code+sqs_code+lambda_code
  63. valuea = valueb.replace("\n","")
  64. updata_db(m_key,valuea)
  65. elif "120" == order:
  66. valueb = https_code+lambda_code+sqs_code
  67. valuea = valueb.replace("\n","")
  68. updata_db(m_key,valuea)
  69. elif "201" == order:
  70. valueb = lambda_code+sqs_code+https_code
  71. valuea = valueb.replace("\n","")
  72. updata_db(m_key,valuea)
  73. elif "210" == order:
  74. valueb = lambda_code+https_code+sqs_code
  75. valuea = valueb.replace("\n","")
  76. updata_db(m_key,valuea)
  77. return {
  78. 'statusCode': 200,
  79. 'body': json.dumps('Hello from Lambda!')
  80. }
  81. # 2、API-gateway lambda
  82. import json
  83. import boto3
  84. import time
  85. def lambda_handler(event, context):
  86. time.sleep(2)
  87. body = json.loads(event['body'])
  88. val = body["Message"]
  89. key = body['MessageAttributes']['mykey']['Value']
  90. print("FORMHTTPS:" + key + ":" + val)
  91. ddb= boto3.client('dynamodb')
  92. table_name = 'found'
  93. ddb.update_item(
  94. TableName=table_name,
  95. ExpressionAttributeValues={
  96. ':y': {
  97. 'S': val,
  98. }
  99. },
  100. Key={
  101. 'm_key': {
  102. 'S': key,
  103. }
  104. },
  105. UpdateExpression='SET https = :y',
  106. ReturnValues="UPDATED_NEW"
  107. )
  108. return {
  109. 'statusCode': 200,
  110. 'body': json.dumps('Hello from Lambda!')
  111. }
  112. # 3、lambda - lambda
  113. import json
  114. import boto3
  115. def lambda_handler(event, context):
  116. sec = event['Records'][0]['Sns']['MessageAttributes']
  117. mes = event['Records'][0]['Sns']['Message']
  118. m_key = sec['mykey']['Value']
  119. order = sec['order']['Value']
  120. a1 = order.replace(":", "")
  121. a2 = a1.replace("SQS", '')
  122. a3 = a2.replace("HTTPS","")
  123. a4=a3.replace("LAMBDA","")
  124. a5=a4.replace(" ","")
  125. print("ORDER:"+ m_key + ":" + a5)
  126. print("FORMLAMBDA:" + m_key + ":" + mes)
  127. ddb= boto3.client('dynamodb')
  128. tableName = 'found'
  129. item = { 'm_key': {'S': m_key},
  130. 'order': {'S': a5},
  131. 'lambda': {'S': mes}
  132. }
  133. ddb.put_item(TableName = tableName, Item = item)