Error Reporting for AWS Lambda

DZone 's Guide to

Error Reporting for AWS Lambda

Doing serverless? Everyone is, right? But do you know how to handle errors correctly? Find out how to configure AWS Lambda for less opaque error handling.

· Cloud Zone ·
Free Resource

AWS Lambda is a serverless offering from Amazon Web Services to help teams build software without worrying about the underlying machine. Serverless hosting means you're paying just for what you use based on workload.

Not having to worry about the underlying server really is adopting "the cloud" as it was always meant to be: automatic scaling, paying for what you use, and minimizing costs.

However, it does mean also being a Cloud Native. What does that mean? It means you'll want to leverage other cloud products, such as Raygun, because you're not going to have a log directory on the machine. It means connecting services, rather than configuring servers. Overall, it makes managing your infrastructure a thing of the past.

Error Reporting For AWS Lambda Using Raygun

So you've decided to give AWS Lambda a test drive. Maybe you're building an API endpoint, but you want to ensure that you're recording any software errors so that you can fix problems as soon as they appear.

What Makes Raygun Great for AWS Lambda?

Firstly, because you don't have direct access to system resources, your only real option for storing error logs is to use a cloud provider. Raygun's cloud offering is used to track software errors directly with Lambda.

When something goes wrong with your code, Raygun will kick in and send the details to a hosted service. It will also notify you in real time to the fault so you can resolve the issue.

What About the Cost With AWS Lambda?

This is where a service like Raygun really shines. AWS Lambda charges on request count and compute time. The great thing about this is that when Raygun initializes, it is a single line of code that attaches to listen for unhandled errors. This means Raygun will have next to zero impact on the costs unless you're generating errors where Raygun has to collect information to send off.

Reporting Errors From AWS Lambda

Integrating Raygun is really easy. In this example, I'll use the Node support that it has (though Raygun does support most programming languages quite happily). I'm using the default Lambda boilerplate example, with Raygun reporting errors.

process.env.NODE_CONFIG_DIR = process.env["LAMBDA_TASK_ROOT"];

// Include Raygun (note that the library and dependencies has been added to the packages directory)
var raygun = require('raygun');

exports.handler = (event, context, callback) => {


      var a = 0;
      var b = a.DoSomeMagicThing();
      console.log('Should never reach this');
    catch (e) {    
      console.log('Sending to Raygun');
      // Send the error to Raygun
      var raygunClient = new raygun.Client().init({ apiKey: 'YOUR_API_KEY_GOES_HERE' });

    callback(null, 'All done..');

That's a Wrap

Together, Amazon Lambda and Raygun can help give you a great serverless product offering. You can keep costs under control while not having to manage servers and infrastructure manually.

aws, error handling, node js

Published at DZone with permission of John-Daniel Trask , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}