-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathload_followers.py
44 lines (38 loc) · 1.38 KB
/
load_followers.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
import time
import cfnresponse
import os
import boto3
import twitter
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.getenv('DDB_TABLE'))
read_units = int(os.getenv('DDB_READ_UNITS'))
api = twitter.Api(
os.getenv('CONSUMER_KEY'),
os.getenv('CONSUMER_SECRET'),
os.getenv('ACCESS_TOKEN_KEY'),
os.getenv('ACCESS_TOKEN_SECRET'),
)
def lambda_handler(event, context):
try:
if event['RequestType'] in ['Delete', 'Update']:
cfnresponse.send(event, context, cfnresponse.SUCCESS)
return
timestamp = int(time.time())
num_followers = 0
with table.batch_writer() as batch:
for follower in api.GetFollowerIDs():
num_followers += 1
batch.put_item(
Item={'follower': follower, 'timestamp': timestamp}
)
resp_data = {'followers_loaded': num_followers, 'elapsed': int(time.time()) - timestamp}
cfnresponse.send(event, context, cfnresponse.SUCCESS, resp_data)
# When we're done lets lower the write capacity units to a reasonable level
table.update(
ProvisionedThroughput={
"ReadCapacityUnits": read_units,
"WriteCapacityUnits": 5
})
except Exception as ex:
print ex
cfnresponse.send(event, context, cfnresponse.FAILED, {'error': str(ex)})