new-SNS-SQS-Lambda-HTTPS.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #1 https
  2. import boto3
  3. import json
  4. table_name = 'hehe'
  5. def updata_ddb(mykey,order,msg):
  6. client = boto3.client('dynamodb')
  7. # response = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  8. # item = response.get("Item")
  9. # # if item == None and item.get('https'):
  10. client.update_item(TableName=table_name,Key={'mykey': {'S':mykey}},UpdateExpression='SET https = :https, orderss = :orderss',
  11. ExpressionAttributeValues={
  12. ':https': {'S': msg},
  13. ':orderss':{'S': order},})
  14. # else:
  15. # pass
  16. def lambda_handler(event, context):
  17. message = json.loads(event['body'])['Message']
  18. mykey = json.loads(event['body'])['MessageAttributes']['mykey']['Value']
  19. order_orgain = json.loads(event['body'])['MessageAttributes']['order']['Value']
  20. order = f"{order_orgain.split(' ')[0][-1]}{order_orgain.split(' ')[1][-1]}{order_orgain.split(' ')[2][-1]}"
  21. print(mykey,order,message)
  22. updata_ddb(mykey=mykey,order=order,msg=message)
  23. return {
  24. 'statusCode': 200,
  25. 'body': json.dumps('Hello from Lambda!')
  26. }
  27. # SQS
  28. import boto3
  29. import json
  30. import time
  31. import pymysql
  32. client = boto3.client('dynamodb')
  33. table_name = 'hehe'
  34. def updata_ddb(mykey,msg):
  35. client = boto3.client('dynamodb')
  36. client.update_item(TableName='hehe',
  37. Key={'mykey': {'S':mykey}},
  38. UpdateExpression='SET sqs = :sqs',
  39. ExpressionAttributeValues={
  40. ':sqs': {
  41. 'S': msg},
  42. })
  43. def exec_sql(mykey,myvalue):
  44. conn2 = pymysql.connect(
  45. host='aurora-db.cluster-chxg6aynimx1.rds.cn-northwest-1.amazonaws.com.cn',
  46. port=3306,
  47. user='admin',
  48. passwd='12345678',
  49. db='cmd',
  50. charset='utf8mb4')
  51. cs2 = conn2.cursor()
  52. ex_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{mykey}", "{myvalue}");'
  53. cs2.execute(ex_sql)
  54. conn2.commit()
  55. cs2.close()
  56. conn2.close()
  57. def sorta(num,sqs,https,lambdaa):
  58. if num == '012':
  59. return(f"{sqs}{https}{lambdaa}")
  60. elif num == '021':
  61. return(f"{sqs}{lambdaa}{https}")
  62. elif num == '102':
  63. return(f"{https}{sqs}{lambdaa}")
  64. elif num == '120':
  65. return(f"{lambdaa}{sqs}{https}")
  66. elif num == '201':
  67. return(f"{https}{lambdaa}{sqs}")
  68. elif num == '210':
  69. return(f"{lambdaa}{https}{sqs}")
  70. def lambda_handler(event,context):
  71. message = json.loads(event['Records'][0]['body'])['Message']
  72. mykey = json.loads(event['Records'][0]['body'])['MessageAttributes']['mykey']['Value']
  73. updata_ddb(mykey=mykey,msg=message)
  74. # time.sleep(1)
  75. try:
  76. res = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  77. sqs_code = message
  78. https_code = res['Item']['https']['S']
  79. order_code = res['Item']['orderss']['S']
  80. lambda_code = res['Item']['lambda']['S']
  81. myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  82. exec_sql(mykey=mykey,myvalue=myvalue)
  83. except:
  84. time.sleep(1)
  85. res2 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  86. sqs_code = message
  87. https_code = res2['Item']['https']['S']
  88. order_code = res2['Item']['orderss']['S']
  89. lambda_code = res2['Item']['lambda']['S']
  90. myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  91. exec_sql(mykey=mykey,myvalue=myvalue)
  92. finally:
  93. time.sleep(2)
  94. res3 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  95. sqs_code = message
  96. https_code = res3['Item']['https']['S']
  97. order_code = res3['Item']['orderss']['S']
  98. lambda_code = res3['Item']['lambda']['S']
  99. myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  100. exec_sql(mykey=mykey,myvalue=myvalue)
  101. # 3. SNS
  102. import boto3
  103. import json
  104. import time
  105. import pymysql
  106. client = boto3.client('dynamodb')
  107. table_name = 'hehe'
  108. def updata_ddb(mykey,msg):
  109. client = boto3.client('dynamodb')
  110. client.update_item(TableName='hehe',
  111. Key={'mykey': {'S':mykey}},
  112. UpdateExpression='SET lambda = :lambda',
  113. ExpressionAttributeValues={
  114. ':lambda': {
  115. 'S': msg},
  116. })
  117. def exec_sql(mykey,myvalue):
  118. conn2 = pymysql.connect(
  119. host='aurora-db.cluster-chxg6aynimx1.rds.cn-northwest-1.amazonaws.com.cn',
  120. port=3306,
  121. user='admin',
  122. passwd='12345678',
  123. db='cmd',
  124. charset='utf8mb4')
  125. cs2 = conn2.cursor()
  126. ex_sql = f'INSERT INTO kv (mykey, myvalue) VALUES ("{mykey}", "{myvalue}");'
  127. cs2.execute(ex_sql)
  128. conn2.commit()
  129. cs2.close()
  130. conn2.close()
  131. def sorta(num,sqs,https,lambdaa):
  132. if num == '012':
  133. return(f"{sqs}{https}{lambdaa}")
  134. elif num == '021':
  135. return(f"{sqs}{lambdaa}{https}")
  136. elif num == '102':
  137. return(f"{https}{sqs}{lambdaa}")
  138. elif num == '120':
  139. return(f"{lambdaa}{sqs}{https}")
  140. elif num == '201':
  141. return(f"{https}{lambdaa}{sqs}")
  142. elif num == '210':
  143. return(f"{lambdaa}{https}{sqs}")
  144. def lambda_handler(event,context):
  145. mykey = event['Records'][0]['Sns']['MessageAttributes']['mykey']['Value']
  146. message = event['Records'][0]['Sns']['Message']
  147. updata_ddb(mykey=mykey,msg=message)
  148. # time.sleep(2)
  149. # try:
  150. # res = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  151. # sqs_code = res['Item']['sqs']['S']
  152. # https_code = res['Item']['https']['S']
  153. # order_code = res['Item']['orderss']['S']
  154. # lambda_code = res['Item']['lambda']['S']
  155. # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  156. # exec_sql(mykey=mykey,myvalue=myvalue)
  157. # except:
  158. # time.sleep(4)
  159. # res2 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  160. # sqs_code = res2['Item']['sqs']['S']
  161. # https_code = res2['Item']['https']['S']
  162. # order_code = res2['Item']['orderss']['S']
  163. # lambda_code = res2['Item']['lambda']['S']
  164. # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  165. # exec_sql(mykey=mykey,myvalue=myvalue)
  166. # finally:
  167. # time.sleep(5)
  168. # res3 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  169. # sqs_code = res3['Item']['sqs']['S']
  170. # https_code = res3['Item']['https']['S']
  171. # order_code = res3['Item']['orderss']['S']
  172. # lambda_code = res3['Item']['lambda']['S']
  173. # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  174. # exec_sql(mykey=mykey,myvalue=myvalue)
  175. # if sqs_code is not None and https_code is not None and order_code is not None and lambda_code is not None:
  176. # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  177. # exec_sql(mykey=mykey,myvalue=myvalue)
  178. # else:
  179. # time.sleep(1)
  180. # res4 = client.get_item(TableName='hehe',Key={'mykey':{'S':mykey}})
  181. # sqs_code = res4['Item']['sqs']['S']
  182. # https_code = res4['Item']['https']['S']
  183. # order_code = res4['Item']['orderss']['S']
  184. # lambda_code = res4['Item']['lambda']['S']
  185. # myvalue = sorta(num=order_code,sqs=sqs_code,https=https_code,lambdaa=lambda_code)
  186. # exec_sql(mykey=mykey,myvalue=myvalue)
  187. # return {
  188. # 'statusCode': 200,
  189. # 'body': json.dumps('Hello from Lambda!')
  190. # }