Created
November 18, 2024 22:41
-
-
Save gtrummell/8c1a6759d0ffe341a2247ce033e81c6f to your computer and use it in GitHub Desktop.
AWS Lambda example providing HTTP-type responses
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
| import logging | |
| import json | |
| # Configure logging | |
| logger = logging.getLogger() | |
| logger.setLevel(logging.INFO) | |
| def context_to_dict(context): | |
| """Convert the Lambda context object to a serializable dictionary.""" | |
| context_dict = { | |
| 'function_name': context.function_name, | |
| 'function_version': context.function_version, | |
| 'invoked_function_arn': context.invoked_function_arn, | |
| 'memory_limit_in_mb': context.memory_limit_in_mb, | |
| 'aws_request_id': context.aws_request_id, | |
| 'log_group_name': context.log_group_name, | |
| 'log_stream_name': context.log_stream_name, | |
| } | |
| # Include identity and client context if they exist | |
| if hasattr(context, 'identity') and context.identity is not None: | |
| context_dict['identity'] = { | |
| 'cognito_identity_id': context.identity.cognito_identity_id, | |
| 'cognito_identity_pool_id': context.identity.cognito_identity_pool_id, | |
| } | |
| if hasattr(context, 'client_context') and context.client_context is not None: | |
| client_context = context.client_context | |
| context_dict['client_context'] = { | |
| 'client': client_context.client, | |
| 'custom': client_context.custom, | |
| 'env': client_context.env, | |
| } | |
| return context_dict | |
| def lambda_handler(event, context): | |
| # Log the received event and context | |
| logger.info('Received event: %s', json.dumps(event)) | |
| logger.info('Lambda context: %s', context_to_dict(context)) | |
| event_type = event.get('type') | |
| logger.info('Processing event type: %s', event_type) | |
| try: | |
| if event_type == 'success': | |
| response = { | |
| 'statusCode': 200, | |
| 'body': 'success (200)' | |
| } | |
| elif event_type == 'client error': | |
| response = { | |
| 'statusCode': 400, | |
| 'body': 'client error (400)' | |
| } | |
| elif event_type == 'redirect': | |
| response = { | |
| 'statusCode': 300, | |
| 'body': 'redirect (300)' | |
| } | |
| elif event_type == 'server error': | |
| response = { | |
| 'statusCode': 500, | |
| 'body': 'server error (500)' | |
| } | |
| elif event_type == 'function context': | |
| response = { | |
| 'statusCode': 200, | |
| 'body': json.dumps(context_to_dict(context)), | |
| 'headers': { | |
| 'Content-Type': 'application/json' | |
| } | |
| } | |
| else: | |
| response = { | |
| 'statusCode': 400, | |
| 'body': 'Unknown event type' | |
| } | |
| except Exception as e: | |
| logger.error('Error processing event: %s', e) | |
| response = { | |
| 'statusCode': 500, | |
| 'body': 'Internal server error' | |
| } | |
| # Log the response | |
| logger.info('Response: %s', response) | |
| return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment