Learn how easily you can harness serverless technology by building your first AWS Lambda function with this simple tutorial.
On the Progress Corticon team, we love serverless technologies. This blog is the start of a series on Serverless in general as well as on application design with Corticon rules and decision services in Serverless environments.
To kick things off, we will start with AWS Lambda functions. Let’s see how easy it is to create your first function.
Creating an AWS Lambda Function
If you don't have an account on AWS, you can get free access simply by creating one here. You will get 1 million Lambda function calls a month for free (See aws.amazon.com/free for details).
First, login to AWS console at this URL: https://aws.amazon.com/console/. Click on Lambda under Compute or simply search Lambda in the services search field.
Now click the button labeled "Create function." You will get to the screen below where you enter a function name (firstFunction) and select Node JS 12. Then click Create at the bottom right of the screen.
Choosing Node.js allows us to program the serverless function with JavaScript. I'm illustrating how to use functions with JavaScript as it is a very common language but feel free to use another language if you are more comfortable with it, everything we cover here apply to other languages.
You will get to the following screen:
Scroll down to the source code and replace “Hello from Lambda” with “Hello from my first function.”
Click Save at to top right and then click Test. You will get to a screen to create a test event to be passed to your function. At this stage we are not using anything from the event so just enter an event name and click Create.
Now you will be back to the editor screen, click Test again. That will run your function with the test event you just created. You will see a successful execution and its result will be displayed in a new tab called “Execution Result,” under the source code.
You will see the result of the execution as well as the unique request ID and the log.
Now let's see how we pass data to the function:
Change the code as highlighted below and rerun the test:
Now you know how to pass data in and get results out.
The Benefits of Your First Lambda Function
Congratulations, you have run your first Lambda function at no cost in your free tier account. If you were to run this in a paid account it would cost $0.0000002083 (As of March 2020). To put this number in perspective: If you were to invoke that function a million times it would cost you 21 cents!!! And even better, you didn't have to set up any machines or instances. The function will auto-scale on demand automatically and, as importantly, when there is no traffic (no invocation) it won't cost you anything (this is often called pay-as-use or pay-as-go).
These are extremely important properties of serverless environments and some of the reasons why people choose them over more traditional technologies.
What about using Microsoft Azure?
In the next blog post we see how we can achieve the same with Microsoft Azure platform.
Thierry Ciot
Thierry Ciot is a Software Architect on the Corticon Business Rule Management System. Ciot has gained broad experience in the development of products ranging from development tools to production monitoring systems. He is now focusing on bringing Business Rule Management to Javascript and in particular to the serverless world where Corticon will shine. He holds two patents in the memory management space.