AWS Lambda environment variables and Raygun

| 6 min. (1143 words)

In our previous article on AWS Lambda and Raygun Crash Reporting, we covered sending exception data from an AWS Lambda function written in Node.js. Recently AWS added support for environment variables to be passed into Lambda functions. These environment variables allow you to set certain constants and then access them in your Lambda functions.

This new feature helps our customers by allowing you to specify your Raygun Crash Reporting API key in the AWS Lambda environment variables instead of hard coding it into your Lambda function. In this article I’ll be demonstrating how to setup our environment variables in AWS Lambda and then how to pass along custom data with other environment variables.

Getting Started

Step 1.

We will be using Node.js again for our example so the first thing we need to do is create an initial Node project. Create a new directory called awsnodeexample via command line.

mkdir awsnodeexample

Then switch to the new directory and create an index.js file to hold the sample code:

cd awsnodeexample/

touch index.js

Next, use Node Package Manager (npm) to create the Raygun Crash Reporting integration and install the necessary modules. This is the same process you would use to integrate Raygun with any Node.js application:

npm install raygun

If you are interested you can review our open source Node.js provider code in our Github repo.

Step 2.

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

// 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: process.env.RAYGUN_API_KEY });
      raygunClient.send(e);
    }

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

The important distinction between this code sample and the previous article’s example is how we access the API key. Using process.env.RAYGUN_API_KEY  we will be able to access the environment variable for the Raygun API key that we will set up here shortly.

Uploading our function to AWS Lambda

Now that we have our function and modules ready to go, we’ll need to get them into AWS.  Since we need the modules created when we ran npm install raygun  we will have to create a .zip file with both our index.js  and node_modules  files.  Both Windows and Mac operating systems have methods for creating .zip files so I won’t cover that portion here.

For the sake of my example, I created a .zip file named awsnodeexample.zip

Once you have created the .zip file you’ll need to upload it to AWS Lambda through either their Lambda API or via the AWS Lambda dashboard. I’ll use the AWS Lambda dashboard here, but for more information on the Lambda API you can check the documentation.

Step 1.

Start by signing into AWS and navigating to the AWS Lambda service.  Then click on ‘Create a new function’:

Step 2.

Now you will be asked to select a blueprint for your function. Since we’ve built our own already, we’ll just choose ‘Blank Function’ and then click ‘Next’:

On the next screen, you’ll be prompted to set up what the Lambda function will trigger from. For now, just click ‘Next’:

Step 3.

The second to last step of our upload takes place on the ‘Configure Function’ screen. Here, there are few different things to set up.

Firstly, AWS Lambda will ask you to create a name, give a description, and confirm the runtime of your function. Here I put awsnodeexample , entered a description, and confirmed that this is a Node.js 4.3 runtime:

Below the ‘Configure function’ section you will use the dropdown menu to select ‘Upload a .ZIP file’:

Once you’ve chosen the ‘Upload a .ZIP file’ option the section will change and allow you to specify which file you want to upload and give you space to enter in any environment variables you want. Here I’ve chosen my awsnodeexample.zip  file and set my RAYGUN_API_KEY :

Step 4.

The final step concerns the Lambda function handler and roles. You will want to enter in a handler name that reflects the following syntax:

<sample_code_file_name>.handler

In our example we named the file holding our code index.js  so our handler needs to be called index.handler .  If the names don’t match up, Lambda will not work and will alert you to the mismatched names.

The ‘Role’ box of this section references the permissions this function will have. In this case I chose ‘Test Harness Permissions’ because this is just a basic example and not meant for production:

The rest of the options are not important for the scope of this article, so we can ignore them for now and press ‘Next’ at the bottom of the screen.

Our last setup step is to review the information we just input regarding our new AWS Lambda function. If all the information looks correct, click on the ‘Create function’ button at the bottom of the window:

After clicking ‘Create function’ you’ll be taken to the AWS Lambda dashboard for your new function. Here you can clickTest’ to see if the code works:

If you’ve entered in the code and API key correctly, you should see the following success message and then see the test error pop up inside the Crash Reporting dashboard in your Raygun application:

Sending Other Environment Variables

Now that we have our Raygun API key environment variable setup and working, we can go a bit further and send along other environment variables when we track our exception.

To get started, let’s modify the existing code in our AWS Lambda dashboard to add a second parameter to raygunClient.send() :

raygunClient.send(e, { ‘other_variable’: process.env.OTHER_VARIABLE });

This sends the key value pair in the second argument as a ‘Custom Data’ payload when the exception is tracked. Here we’re using ‘other_variable’ but any value can be sent along.

Next we have to add the new environment variable to our function. We can do this just below the code editor in our AWS Lambda function’s dashboard:

Now click on ‘Save and test’ at the top of our dashboard window to save the changes and test them:

If we go to our Raygun Crash Reporting dashboard and look into the error, we’ll see the custom data passed through with the exception in the ‘Custom’ tab of the error report:

And that’s it!

As you can see, the addition of environment variables will make the setup and scaling of their AWS Lambda functions much easier. The sample code is available on GitHub if you would like to take a closer look or clone the repo for your own testing.

Do you have any questions about how Raygun Crash Reporting works with AWS Lambda functions? Leave a comment below!