I've deployed a flask application on AWS lambda. To perform error tracking I want to integrate Sentry into the application. Here is my setup for the application
def _handle_error(e):
code = 500
msg = 'Not Found'
if isinstance(e, HTTPException):
code = 404
elif isinstance(e, ValueError):
msg = str(e)
logging.exception(e)
if os.getenv('SENTRY_DSN'):
event_id = sentry_sdk.capture_exception(e)
logging.info(f'Error event was sent to sentry, event id: {event_id}')
return jsonify({'error': msg}), code
def create_app():
app = Flask(__name__)
CORS(app, support_credentials=True)
if os.getenv('SENTRY_DSN'):
sentry_logging = LoggingIntegration(
level=logging.DEBUG, # Capture info and above as breadcrumbs
event_level=logging.ERROR # Send errors as events
)
sentry_sdk.init(dsn=os.getenv('SENTRY_DSN'),
integrations=[FlaskIntegration(), sentry_logging])
app.register_blueprint(bp_routes)
app.register_error_handler(Exception, _handle_error)
I use the custom error handler in flask to be able to return a generic error message without any sensitive information. When capturing the error I'm trying to manually create an event in Sentry
event_id = sentry_sdk.capture_exception(e)
Running the the code locally with python application.py
everything works, the event is created in Sentry with the expected values.
When deploying the same code to lambda the event is no longer generated.
Also when I remove the custom flask error handler
# app.register_error_handler(Exception, _handle_error)
and deploy it to lambda the event IS generated!
Why is it the capture_exception
not working on lambda?
Using any web framework under AWS Lambda additionally requires the installation of the AWS Lambda integration
For services similar to AWS Lambda, the generic serverless integration might become useful.