Testing AWS Lambda Functions the Easy Way

This is a short article about building AWS Lambda test data. It specifically covers an easy way to create test information. It will be most useful to readers who are already familiar with AWS Lambda functions. This technique is most useful when there is already a client that feeds data to the Lambda function.

Introduction

The AWS Lambda console provides the ability to configure a test event.

Lambda console and available actions

Selecting “Configure test event” opens a new window that has a drop down. The items in the drop down are sample event templates that can be consumed by the Lambda in order to test its functionality.

“Input test event” window with sample event templates

Each of the examples event templates is just a JSON object to be consumed by a Lambda

Implementation

My particular workflow inevitably puts me in the position that I already built the client functionality that sends a JSON object to the server to be eventually consumed by a Lambda function. In that case, it makes sense to use one of the request objects to configure my Lambda test.

Assuming the client application sends the correct data, we just need to:

  1. See what the Lambda received
  2. Turn that information into valid JSON
  3. Paste the JSON into the “Input test event” window

This can be done by using the util library to console log the entire event invoking the Lambda. AWS already makes the util available, so no need to download it. Just include it in any Lambda using the following code:

const util = require(‘util’);

With util you can console log the entire event that the Lambda receives with the following line of code:

console.log(“event: “, util.inspect(event, { showHidden: false, depth: null }));

Next, invoke the Lambda using the client application. Then go to the CloudWatch Log Group for that Lambda and open up the most recently logged event. It should contain the text for the entire event that the Lambda received. Note that the event is shown as a JavaScript object.

Sample event printout from Cloudwatch log

The Chrome Developer console can be used to convert the JS object to valid JSON by using JSON.stringify. Copy the event from CloudWatch (making sure to copy the entire “event” object). Type JSON.stringify( in the command line of the Chrome Developer Console. Paste the event into the command line, and then close the parenthesis.

JSON.stringify the event

The result will be a string containing valid JSON of the event object.

Valid JSON of the event

Now, its just a matter of copying the JSON string without the closing quotation marks, and pasting that into the “Input test event” window.

Finally, click the “Save and test” button to test-drive your new test.