-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws_lambda_function.py
114 lines (99 loc) · 3.29 KB
/
aws_lambda_function.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
Lambda Function to give random name from list of friends,
reads & writes config file into Amazon S3 bucket &
sends notification using Amazon SNS
"""
# __author__ = "Ameya Joshi"
# __credits__ = ["Ameya Joshi"]
# __maintainer__ = "Ameya Joshi"
# __email__ = "joshiamey22@gmail.com"
import json
import random
import boto3 # Python SDK for AWS
s3 = boto3.client('s3')
topic = boto3.client('sns')
bucket = 'your-bucket-name'
key = 'config.json'
def give_random_name(names):
"""
Select random name from names list
:param names: List having names of friends
"""
return random.choice(names)
def write_into_s3_file(writeobj):
"""
Upload new data to a bucket
:param writeobj: updated JSON data to upload
"""
try:
s3.put_object(
Body=json.dumps(writeobj),
Bucket=bucket,
ACL='public-read-write',
ContentType='application/json',
Key=key)
except Exception as err:
print("Couldn't put object into S3 bucket: {}".format(err))
def read_object_from_s3():
"""
Gets an object from bucket & reads content from the object
"""
try:
response = s3.get_object(Bucket=bucket, Key=key)
content = response['Body']
return json.loads(content.read())
except Exception as err:
print("Couldn't get object from S3 bucket: {}".format(err))
def publish_msg(message):
"""
Publish message to the topic created using Amazon SNS
:param message: Message to publish
"""
try:
resp = topic.publish(
TargetArn="your-topic-arn",
Message=json.dumps({'default': message}),
MessageStructure='json')
print("Published message successfully")
return resp
except Exception as error:
print("Couldn't publish message: {}".format(error))
return error
def lambda_handler(event, context):
# TODO implement
bc_friends = ['Ameya', 'Ashish', 'Ashutosh', 'Mandar', 'Nakul', 'Ojas', 'Rohit', 'Tanmay']
def check_availability(nam, names_lst):
print("Name:: {}".format(nam))
if nam in names_lst:
print("name available in the list")
noun = give_random_name(bc_friends)
name_list = jsonobj['names']
n = check_availability(noun, name_list)
return str(n)
else:
return str(nam)
jsonobj = read_object_from_s3()
print(jsonobj)
c = jsonobj['count']
if c == 8:
jsonobj['count'] = 0
print("8 months --> 1 round of BC completed")
jsonobj['names'] = []
write_into_s3_file(jsonobj)
alert = "8 months --> 1 round of BC completed"
resp = publish_msg(alert)
else:
name = give_random_name(bc_friends)
names_list = jsonobj['names']
star = check_availability(name, names_list)
for k, v in jsonobj.items():
if k == 'names':
jsonobj['names'].append(star)
jsonobj['count'] = c + 1
write_into_s3_file(jsonobj)
message = "Hi, Send BC amount to {} for this month".format(star)
resp = publish_msg(message)
return {
'statusCode': 200,
'body': json.dumps(resp)
}