Created
November 18, 2024 23:00
-
-
Save gtrummell/1f898aeca2d09b2cd3069a084307158f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, jsonify | |
| import boto3 | |
| import json | |
| app = Flask(__name__) | |
| # Initialize the AWS Lambda client | |
| lambda_client = boto3.client('lambda', region_name='your-region') | |
| def invoke_lambda_function(payload): | |
| response = lambda_client.invoke( | |
| FunctionName='your_lambda_function_name', | |
| InvocationType='RequestResponse', | |
| Payload=json.dumps(payload) | |
| ) | |
| response_payload = response['Payload'].read().decode('utf-8') | |
| return json.loads(response_payload) | |
| @app.route('/success') | |
| def success(): | |
| payload = {'type': 'success'} | |
| lambda_response = invoke_lambda_function(payload) | |
| return jsonify({'message': lambda_response.get('body')}), lambda_response.get('statusCode', 500) | |
| @app.route('/clienterror') | |
| def client_error(): | |
| payload = {'type': 'client error'} | |
| lambda_response = invoke_lambda_function(payload) | |
| return jsonify({'message': lambda_response.get('body')}), lambda_response.get('statusCode', 500) | |
| @app.route('/servererror') | |
| def server_error(): | |
| payload = {'type': 'server error'} | |
| lambda_response = invoke_lambda_function(payload) | |
| return jsonify({'message': lambda_response.get('body')}), lambda_response.get('statusCode', 500) | |
| @app.route('/redirect') | |
| def redirect_event(): | |
| payload = {'type': 'redirect'} | |
| lambda_response = invoke_lambda_function(payload) | |
| return jsonify({'message': lambda_response.get('body')}), lambda_response.get('statusCode', 500) | |
| @app.route('/', defaults={'path': ''}) | |
| @app.route('/<path:path>') | |
| def unknown_event(path): | |
| payload = {'type': path} if path else {} | |
| lambda_response = invoke_lambda_function(payload) | |
| return jsonify({'message': lambda_response.get('body')}), lambda_response.get('statusCode', 500) | |
| if __name__ == '__main__': | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment