Error reporting for AWS Lambda

| 4 min. (671 words)

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 minimising 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 end point, 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 by some of the worlds best businesses to track their software errors, and you can use it directly with Lambda.

When something goes wrong with your code, we will kick in and send the details to our hosted service. We’ll also notify you in real time to the fault so you can resolve the issue.

What about the cost with AWS Lambda?

Great question! 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

To integrate Raygun is really easy. In this example, I’ll use the Node error reporting support that we have (though Raygun does support most programming languages quite happily). In this example, I’m using the default Lambda boilerplate example, with Raygun reporting errors.

Getting started

As we are using Node for our example the first thing we need to do is create an initial Node module in a file we will call index.js and then include Raygun as a library for our use. This is the same process you use to integrate Raygun with any Node application.

To integrate with Raygun we will run:

npm install raygun

If you are interested you can review our open source provider code on Github

The next step is to implement a simple function with error handling in our index.js file.

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..');
};

Uploading our application to Lambda

Now that we have integrated Raygun and implemented a basic application my folder structure now looks like this:

Lambda allows us to upload applications by creating a zip archive of this folder structure and then uploading it either via the Lambda API or using the Amazon AWS Lambda console.

Once uploaded we can execute our Lambda function in the normal fashion.

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.

You can sign up for a free 14 day trial of Raygun to test out with Lambda, and let me know what you think by commenting below!