This repository was archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_module.py
More file actions
57 lines (44 loc) · 1.5 KB
/
Copy pathlambda_module.py
File metadata and controls
57 lines (44 loc) · 1.5 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import print_function
import json
import boto3
s3_client = boto3.client("s3")
def get_template(filename):
response = s3_client.get_object(
Bucket='config-data-bucket',
Key='template/{}'.format(filename)
)
return response['Body'].read()
def get_objects_list():
return [json.loads(
s3_client.get_object(
Bucket='config-data-bucket', Key=item['Key']
)['Body'].read()
) for item in s3_client.list_objects_v2(
Bucket='config-data-bucket', Prefix='data'
)['Contents'] if item['Key'].endswith('.json')]
def get_object(id):
response = s3_client.get_object(
Bucket='config-data-bucket',
Key='data/{}.json'.format(id)
)
return json.load(response['Body'])
def lambda_handler(event, context):
print "Event received"
resource = event['path_params']['resource']
method = event['http_method']
id = event['path_params'].get('id')
if resource == 'user' and method=="GET":
base_template = get_template('base.tpl')
user_template = get_template('user.tpl')
if id:
user = get_object(id)
return base_template.format(
body=user_template.format(id=user['id'], name=user['name'])
)
else:
users = get_objects_list()
return base_template.format(
body="".join(
user_template.format(id=user['id'], name=user['name']) for user
in users)
)