This is a simple python decorate for the aws serverless (lambda) functions to make it easy to add email notification (or other SNS handling) for errors and exceptions.

Fred Damstra 9006ab03a0 Generic arn 5 éve
.gitignore c7be7cf1bc Initial commit 6 éve
LICENSE c7be7cf1bc Initial commit 6 éve
README.md cbd5e5ab9a Added ability to enable/disable notifications via an environment variable. 6 éve
example.py e5202333dd Redid function as a decorator. 6 éve
sns_error_notification.py 9006ab03a0 Generic arn 5 éve

README.md

sns_error_notification

Source

The latest version of this code can be found at https://github.com/fdamstra/sns_error_notification

Description

This is a simple python decorate for the aws serverless (lambda) functions to make it easy to add email notification (or other SNS handling) for errors and exceptions.

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 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. Create an issue on this git repo, email me, whatever. I'm very interested in better ways. 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. Grant your lanbda function sns:Publish permission.
  4. Set the environment variable sns_error_topic to your SNS topic's arn.
  5. (Optional) Add the environment variable sns_notifications_enabled and set to either the string true or false to quickly enable or disable email notifications without code changes.
  6. Add from sns_error_notification import sns_error_notification to your handler.
  7. Add the line @sns_error_notification directly before your handler function.

See the example.py for an example.