浏览代码

Initial commit.

Fred Damstra 6 年之前
父节点
当前提交
a12fae0c6d
共有 2 个文件被更改,包括 84 次插入1 次删除
  1. 44 1
      README.md
  2. 40 0
      lambda_function.py

+ 44 - 1
README.md

@@ -1,2 +1,45 @@
 # aws_lambda_python_error_emailer
-Code to place in lambda so that python exceptions and errors will be emailed to you with sufficient details
+This is a simple template you can add to your existing AWS Lambda python code
+so that it will email you details on your exceptions and errors.
+
+You can use CloudWatch to alert on errors, but then you just know that
+something went wrong, not _what_ went wrong. 
+
+I'm sure there are other ways to do this, but I didn't have luck finding them
+and getting the information I needed, so here's what I came up with.
+
+## Caveats
+This could generate a LOT of email. I am not responsible for your email
+administrator's wrath. You could always set up a different subscriber to
+the SNS topic instead of directly emailing.
+
+This won't report syntax errors when loading the code. Make it a best practice
+to run `python3 -m py_compile lambda_function.py` before deploying to python. 
+For that matter, add some unit or doc tests.
+
+It also won't catch errors with deployment of the lambda environment, which
+does happen from time to time.
+
+It will also report errors even if a retry of the script succeeds. See
+[AWS Lambda Retry Behavior](https://docs.aws.amazon.com/lambda/latest/dg/retries-on-errors.html)
+for information on when it may automatically be retried.
+
+Lastly, it won't catch issues with the SNS queue itself. If that isn't 
+set up right, you won't get emails.
+
+## If you know a better way
+Please share it. I'd love to hear it it. I want to see:
+* The full stack trace
+* The event received as input
+* The context (or at least important parts)
+
+Additionally, it needs to handle rates of thousands of executions per
+second.
+
+## Set Up
+1. Configure an SNS topic.
+2. Subscribe to the SNS topic using your email address.
+3. Verify your subscription.
+4. Rename your `lambda_handler` function.
+5. Add the code from `lambda_function.py` to your function and make it the new handler.
+

+ 40 - 0
lambda_function.py

@@ -0,0 +1,40 @@
+#! /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