By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts.
News

Kalepa Open-Sources Safe Init: Simplifying AWS Lambda Error Handling and Monitoring

Learn more about how Safe Init can help developers write more resilient and maintainable Lambda functions.
Kalepa
4 min

At Kalepa, we're passionate about building robust and reliable systems. As a company heavily reliant on serverless architecture, particularly AWS Lambda, we understand the challenges of ensuring the stability and observability of our functions. We're excited to announce the open-sourcing of Safe Init, a powerful Python library that has transformed how we handle errors, monitor performance, and maintain our Lambda functions.

The Journey to Safe Init

As our serverless infrastructure grew (thanks to adding quite a few clients), we encountered common challenges with debugging Lambda functions, especially when dealing with unexpected crashes or timeouts. We wanted to proactively monitor the health of our functions and receive real-time notifications when things went wrong. It’s critically important to us that our Copilot software remains scalable and extremely robust, with industry-leading uptime. Our goal was simple: 0 lambda errors.

Frustrated with existing solutions, our Senior DevOps Engineer, Maciej "Lupin" Wilczyński, developed Safe Init to address these pain points. Thanks to his background in serverless technologies and desire for elegant solutions, Lupin created a library that would become a game-changer for our Lambda error handling and monitoring.

Unveiling the Power of Safe Init

Safe Init is a comprehensive suite of tools that empowers developers to write more resilient and maintainable Lambda functions. Let’s dive into some of its key features:

Seamless Error Handling

One of the most significant benefits of Safe Init is its ability to wrap your Lambda handler with robust error handling and logging. By simply configuring Safe Init as your Lambda's entry point, it automatically captures any unhandled exceptions, ensuring that no error goes unnoticed. 

This means that if an unexpected exception occurs during the execution of your Lambda function, Safe Init will catch it, log the error details, and send notifications to your preferred channels, such as Slack or Sentry. For example, if a particular input triggers a divide-by-zero exception in your code, Safe Init will capture the error, along with the relevant context, and immediately alert your team. This proactive error handling approach has saved us many hours of debugging and has significantly improved the stability and reliability of our functions, as we can quickly identify and resolve issues before they escalate.

Execution Time Tracing

Identifying performance bottlenecks in Lambda functions can be tricky, especially when dealing with complex codebases. Safe Init’s execution time tracing feature is a lifesaver in such scenarios. It automatically tracks the execution time of all function calls within your Lambda handler, providing detailed insights into which parts of your code are consuming the most time. 

Integration with Sentry and Slack

Safe Init seamlessly integrates with popular error tracking and notification tools like Sentry and Slack. However, these integrations are entirely optional, giving users the flexibility to choose the tools that best fit their needs.

With just a few lines of configuration, Safe Init automatically captures exceptions and logs them to Sentry, providing a centralized view of all errors across our Lambda functions. Additionally, it sends informative Slack notifications with error details, ensuring that our team is always aware of any issues in real-time.

Dead-Letter Queue Support

One of the most powerful features of Safe Init is its built-in support for dead-letter queues (DLQ). In the event of a function failure, Safe Init automatically pushes the failing payload to a designated DLQ, allowing for easy retrieval and reprocessing later, ensuring that no data is lost due to transient failures.

This feature proved useful during an outage we experienced in one of our ECS services. The outage caused many of our Lambda functions to time out, but thanks to Safe Init's DLQ support, the events were automatically pushed to the DLQ. Once the timeout issue was resolved, we were able to redrive the events from the DLQ, ensuring that all data was processed successfully without any loss.

Timeout Notifications and Tracing

Lambda timeouts can be frustrating, especially when they occur frequently. Safe Init helps alleviate this pain by sending proactive timeout notifications a configurable number of seconds before the actual timeout occurs. This early warning system has allowed us to investigate and address potential timeout issues before they impact our production environment.

But Safe Init takes it a step further with its timeout tracing capabilities. When a Lambda function times out frequently, we can enable Safe Init’s tracing feature to gain deep insights into which parts of the code are causing the timeouts. The detailed traces provided by Safe Init have been the most effective way we’ve encountered to identify and resolve timeout issues.

Real-World Impact

Since integrating Safe Init into our serverless architecture, we’ve seen a remarkable improvement in the stability and observability of our Lambda functions. Thanks to Safe Init’s comprehensive error handling and monitoring capabilities, we’ve reduced the number of unexpected Lambda crashes to near-zero. We can now catch developers’ errors, such as missing Sentry initialization, as soon as the code is released, preventing potential issues from going unnoticed.

Safe Init has also greatly streamlined our debugging process. Instead of spending hours digging through logs, we can now rely on Safe Init’s detailed error reports and traces to quickly identify and resolve issues. This has significantly reduced our MTTR and has allowed our developers to focus on building new features rather than firefighting.

All told, this benefits our customers (and their customers) by ensuring we maintain best-in-class uptime, so they can count on Copilot to be there for them when they need it.

Migrating to Safe Init

If you’re currently using the Datadog Lambda wrapper for your functions, migrating to Safe Init is a breeze. Simply update your Lambda function’s entry point to safe_init.handler.handler and set the SAFE_INIT_HANDLER environment variable to point to your original handler. 

If you’re not using Datadog, or want to disable the automatic Datadog integration provided by Safe Init, simply set the SAFE_INIT_NO_DATADOG_WRAPPER environment variable to true.

Here’s an example of migrating an existing Datadog-wrapped Lambda handler to Safe Init:

Before:

  • Entry point: datadog_lambda.handler.handler
  • Environment variables:some text
    • DD_LAMBDA_HANDLER: your_module.handler

After:

  • Entry point: safe_init.handler.handler
  • Environment variables:some text
    • SAFE_INIT_HANDLER: your_module.handler

With this simple change, your Lambda function will now benefit from Safe Init’s error handling, monitoring, and tracing capabilities, while still integrating seamlessly with Datadog.

Join the Safe Init Community

Safe Init is now available as an open-source project, and we invite you to join us in making it even better. Whether you’re a seasoned Lambda developer or just getting started with serverless, Safe Init has something to offer. Try it out in your own projects, explore its features, and provide feedback. If you encounter any issues or have ideas for new features, don’t hesitate to open an issue or submit a pull request on our GitHub repository.

We believe that Safe Init has the potential to greatly improve the way developers handle errors and monitor their Lambda functions. By open-sourcing this project, we hope to foster a vibrant community of contributors who share our passion for building robust and reliable serverless applications.

We’ll be actively listening to feedback from the community and working to ensure that Safe Init remains a valuable tool for developers worldwide.

Get Started with Safe Init Today

If you’re ready to take your Lambda error handling and monitoring to the next level, give Safe Init a try. Head over to our GitHub repository, where you’ll find detailed installation instructions. You can also visit our comprehensive documentation with examples to help you get started. We can’t wait to see what you’ll build with Safe Init.

We’re hiring!

At Kalepa, we're always on the lookout for talented individuals who share our passion for building robust and innovative solutions. If you're excited about the potential of Safe Init and want to work with a team that values creativity, collaboration, and technical excellence, we'd love to hear from you. Check out our careers page to explore open positions.

Please check your email for the requested document.
Oops! Something went wrong while submitting the form.

Want to get in touch?

Sign up for our newsletter  for  the latest Copilot news,
features, and events, straight from the source

See how Copilot can deliver better decisions

We'll even show you on your submissions
Set up a demo
Email us: info@kalepa.com
Hassle-free setup
Hands-on training & support
See the value on day one