-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
40 lines (32 loc) · 1.44 KB
/
lambda.py
File metadata and controls
40 lines (32 loc) · 1.44 KB
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
import json
import urllib.parse
import boto3
import pandas as pd
import io
print('Loading function')
s3 = boto3.client('s3')
def send_sms(message):
sns = boto3.client('sns')
number = '+918327717089'
sns.publish(PhoneNumber = '+918327717089', Message=message )
sns.publish(PhoneNumber = '+919581602942', Message=message )
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
print(key)
try:
response = s3.get_object(Bucket=bucket, Key=key)
# print(io.BytesIO(response['Body'].read()).read().decode())
df = pd.read_csv(io.BytesIO(response['Body'].read()),header=0, delimiter=",", low_memory=False)
print(df)
message = ''
message+="Hello, Today's Weather Update for you!!\n"
message+="There weather in {} for today is: {}\n".format(df.loc[0,'city_name'],df.loc[0,'weather'])
message+="Temperature: {}DEG CELSIUS\n".format(df.loc[0,'temperature'])
message+="Wind Speed: {}kmph\n".format(df.loc[0,'wind'])
print(message)
send_sms(message)
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e