Master AWS Lambda Tests: How To Quickly Test Your Serverless Functions
Learn how to easily test your AWS Lambda functions in the console.
Testing your Lambda functions can be confusing.
What do you use? Where do you go?
The simplest solution is usually the best one. AWS provides an integrated testing solution right into your Lambda function code editor.
But it can still be hard to understand how to test your functions, especially the syntax and how to work with the parameters.
In this article, I’ll run you through a quick demo with the most common scenarios you’ll run into when testing your Lambda functions.
Creating A Lambda Function
We’ll start by creating a Lambda function in the AWS console.
Choose the option “Author from scratch”. Name your function, use the Node JS 22 runtime and hit the “create function” button.
Once created, scroll down to the code editor section.
We’ll replace the existing code with the following:
export const handler = async (event) => {
const { userID } = event.pathParameters;
// const { limit } = event.queryStringParameters;
// const { name, age } = JSON.parse(event.body);
const response = {
statusCode: 200,
body: JSON.stringify({
message: `User ID: ${userID}`,
// bodyData: { name, age },
// queryData: { limit }
}),
};
return response;
};
Testing Your Function
Now let’s click on the Test button to start a test to run the function code.
When you click on Test, a dropdown appears, click on “create new test event”.
You’ll see a new tab open to the right:
Give your test event a name “test1”.
Now all of our testing will happen in the JSON editor below.
Path Parameters Testing
Let’s replace the existing JSON with a first test:
{
"pathParameters": {
"userID": "12345"
}
}
This lets us test or mock our pathParameters in the Lambda function. We can pass any value we wish to it in order to test it.
Typically, the pathParameters would come from your endpoint on the frontend, however this provides us a with a clean and easy way to mock the value.
Save this test and run it by clicking the Invoke button. You should see the response show in the integrated terminal:
As expected the value for userID is exactly what we passed in our JSON test.
Let’s see how we can mock the rest of the values typically used in production workloads.
Query String Parameters Testing
To test the queryStringParameters, we need to uncomment line 4 with the “event.queryStringParameters” code as well as line 12 inside the response object “queryData: { limit }”.
We can then add to our JSON test the following:
{
"pathParameters": {
"userID": "12345"
},
"queryStringParameters": {
"limit": 10
}
}
When we run the test we get the following output:
We see the value of “limit” is properly assigned.
event.body Testing
Lastly, let’s test the event.body object.
We’ll pass in values for “name” and “age” like so:
{
"pathParameters": {
"userID": "12345"
},
"queryStringParameters": {
"limit": 10
},
"body": {
"name": "John Doe",
"age": 20
}
}
Don’t forget to uncomment line 5 and 12, deploy the function and run the test.
Now you should see this output:
Now usually the event.body is wrapped as JSON.parse(event.body). Well to support that test we must stringify our body object like so:
{
"pathParameters": {
"userID": "12345"
},
"queryStringParameters": {
"limit": 10
},
"body": "{\"name\":\"John Doe\",\"age\":20}"
}
Notice how we stringify the body value so it can be parsed by JSON.parse().
The bottom line here is any value that comes from “event” in the Lambda root handler function can be mocked by using it inside the test event JSON without using the event object. The value of the event object is already exposed in the JSON test event and we can extract any value it contains directly in the JSON editor.
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can also follow me on Linkedin for valuable daily posts.
Thanks for reading and see you in the next one!