This is just a concept, so you should not consider it as a working use case. At the end of the link is attached to github, if you do not want to go through all the stages manually, as well as the video I recorded, so do not be surprised that I will miss many of the points.
User: id = 'string' name = 'string'
{ "id":"1", "name":"George" }
base.tpl <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Base view</title> </head> <body> {body} </body> </html> user.tpl <a href="#">{id} <span class="label label-success">{name}</span></a><br>
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) )
Source: https://habr.com/ru/post/307692/
All Articles