How to test AWS Lambda function
How to test the serverless AWS lamda function ?
This question pops-up many times during serverless development, as you can't run locally, you need a different method to test your code. fortunately we have very nice tools to completed this important task.
Here's the basic node.js code, deployed from the aws console - basic blueprint
You have at least three different approach, the first one
1., use the AWS lambda managment console
it's quite simple, because you can use the internal testing function of the AWS managment console, search for the function and try to find a test button, you can put some parameters for functions if you need anything, then just press the test and you will see the output on the console.
if you press the down arrow right of the test button, you can configure the function parameters, like this
2., make an API gateway for the function, and you can use any kind of API tester (like: insomnia, postman)
For this to work, you need to create an API Gateway trigger on the lambda function, to be activatable from outside, and with the API Endpoint
- Select the API gateway tigger
- Create an API
- Rest API
- Security: Open
- Press "Add"
Now, the trigger activated, and an API Endpoint is created for the lambda function, you can click on the link, to see the result of the lambda function. Which will be a simple output like this: "Hello from lambda..."
If you want to test the parameters, put the url into an API testing tool, like insomnia, and add some parameters
3., use the integrated visual studio tool
if you install the aws toolkit for visual studio, then you will have many handy tools ready at your disposal. it has a built in testing tool.
Conclusion
I suggest to use the second API testing method with any kind of tool, because it's much closer (technically the same), as when you will use it in production (if you are planning to use as an API, if you want to use it as S3 event, then you need to use the internal tool)