Over a million developers have joined DZone.

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

Download this eBook outlining the critical components of success for SaaS companies - and the new rules you need to play by.  Brought to you in partnership with NuoDB.

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) => {

    console.log('Starting');

    try
    {
      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' });
      raygunClient.send(e);
    }    

    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.

Learn how moving from a traditional, on-premises delivery model to a cloud-based, software-as-a-service (SaaS) strategy is a high-stakes, bet-the-company game for independent software vendors. Brought to you in partnership with NuoDB.

Topics:
node js ,aws ,error handling

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}