"""App entrypoint.""" import json import os import urllib from flask import Flask from flask import jsonify from flask import make_response from flask import request as flask_request app = Flask(__name__) @app.route('/') def hello(): """Hi.""" return 'Hello, cruel world!' @app.route('/2015-03-31/functions//invocations', methods=['POST']) # noqa:E501 def router(function_name): """Forward request to lambda function, re-write error codes.""" func_url = f'http://{function_name}:8080/2015-03-31/functions/function/invocations' # noqa:E501 func_request = urllib.request.Request(func_url, data=flask_request.data) # https://docs.aws.amazon.com/lambda/latest/dg/python-exceptions.html set_function_error_header = False with urllib.request.urlopen(func_request) as func_response: func_response_data = func_response.read() if func_response.code == 200: response_data = json.loads(func_response_data.decode('utf-8')) if 'errorType' in response_data: set_function_error_header = True print(function_name, func_response.code, func_response_data, flush=True) response = make_response( jsonify(response_data), func_response.code ) if set_function_error_header: response.headers['X-Amz-Function-Error'] = True return response if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=os.getenv('PORT', 5000))