forked from talentedmrjones/cloudformation-deep-dive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepop-dynamodb.py
57 lines (43 loc) · 1.38 KB
/
prepop-dynamodb.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
import boto3
import urllib2
import json
class RequestWithMethod(urllib2.Request):
def __init__(self, *args, **kwargs):
self._method = kwargs.pop('method', None)
urllib2.Request.__init__(self, *args, **kwargs)
def get_method(self):
return self._method if self._method else super(RequestWithMethod, self).get_method()
def lambda_handler(event, context):
print(event)
TableName=event['ResourceProperties']['TableARN'].split('table/')[-1]
client = boto3.client('dynamodb')
cities = {
'80014':'Denver',
'80304':'Boulder',
'32601':'Gainesville',
'81601':'Glenwood Springs'
}
print('Populating table')
for zip in cities:
response = client.put_item(
TableName=TableName,
Item={
'zip': {
'S': zip
},
'city':{
'S': cities[zip]
}
}
)
customResponse = {
"Status" : "SUCCESS",
"PhysicalResourceId" : "None",
"StackId" : event['StackId'],
"RequestId" : event['RequestId'],
"LogicalResourceId" : event['LogicalResourceId']
}
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = RequestWithMethod(event['ResponseURL'], method='PUT', data=json.dumps(customResponse))
opener.open(request)
return {}