forked from HadesArchitect/MyVoteAWS
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocessor_lambda.py
84 lines (59 loc) · 2.19 KB
/
processor_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import boto3
import json
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
queue = boto3.resource('sqs', region_name='us-east-1').get_queue_by_name(QueueName="erjan")
table = boto3.resource('dynamodb', region_name='us-east-1').Table('Votes')
def process_message(message):
print('----------process_message----------------------')
print('-------------SQS auto genereated msg------------------------')
print(type(message))
try:
print('----------process_message----------------------')
payload = message['messageAttributes']
vote = payload['vote']['stringValue']
print('-----------------------------------------------')
print('vote ' + str(vote))
print('----------------------------------------')
update_count(vote)
except Exception as e:
print('-----EXCEPTION-----')
print(e)
logging.info(sys.exc_info()[0])
def update_count(vote):
print('update count....')
cur_count = 0
if vote == 'b':
print('vote is b - update...')
response = table.get_item(Key = {'voter':'count'})
item = response['Item']
item['b'] +=1
table.put_item(Item = item)
elif vote == 'a':
print('vote is a - update...')
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD a :incr",
ExpressionAttributeValues={':incr': 1})
def lambda_handler(event,context):
logging.info(event)
logging.info(context)
logging.info('--------inside main-------')
print(event)
print(context)
print('--------inside main-------')
try:
print('--------------------------------------')
print(event)
print('------------------------inside try - queue.receive_messages-------------')
message = event['Records'][0]
try:
process_message(message)
except :
print(' catch it here')
return {'statusCode': 200, 'body': '{"status": "success"}'}
except Exception as e:
logging.error(e)
logging.error(sys.exc_info()[0])
return {'statusCode': 500, 'body': '{"status": "error"}'}