12345678910111213141516171819202122232425262728293031323334353637383940 |
- #! /usr/bin/env python3
- import boto3
- import json
- import traceback
- # Insert original code here:
- # Rename your original lambda_handler() to lambda_handler_noemail()
- def lambda_handler_noemail(event, context):
- # if you start generating too many emails, you can always change
- # the handler back to this.
- return True
- # Make this your caller.
- def lambda_handler(event, context):
- # Set this to an SNS topic you'd like to publish to.
- sns_arn = 'arn:aws:sns:us-east-2:1234567890:lambda-errors',
- try:
- snsclient = boto3.client('sns')
- return lambda_handler_unhandled(event, context)
- except Exception as e:
- subject = f'Execution Error in {context.function_name}'
- estr = str(e)
- tb = traceback.format_exc()
- try:
- event_dump = json.dumps(event, indent=2)
- except:
- event_dump = "ERROR"
- try:
- context_dump = vars(context)
- except:
- context_dump = "ERROR"
- message = f'Exception: {estr}\n\nTraceback: {tb}\n\nEvent: {event_dump}\n\nContext: {context_dump}\n'
- # Only happens if there was an exception
- snsclient.publish(
- Subject=subject,
- TopicArn=sns_arn,
- Message=message
- )
- raise
|