How to Create Your First Serverless/Lambda Functions
Learn how easily you can harness serverless technology by building your first AWS Lambda function with this simple tutorial.
On my team (Corticon), we love Serverless technologies. This blog is the start of a series on Serverless in general as well as on application design with 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 (See next screenshot below). 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 exceed the first million calls per month, it would cost $0.0000002083 (As of September 2020) for each additional calls. To put this number in perspective: If you were to invoke that function a million times it would cost you 21 cents !
For details check these pages:
- https://aws.amazon.com/lambda/pricing/#AWS_Lambda_Pricing
- https://aws.amazon.com/lambda/pricing/#Calculator
And you get even more benefits:
- You didn't have to set up any machines or instances.
- The function will auto-scale on demand automatically when there is more incoming traffic to your function.
- When the traffic decreases, the serverless infrastructure will scale automatically down the needed instances.
- 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 Serverless is such a transformative technology and why people choose them over more traditional technologies.
What about using Microsoft Azure?
In a future post, we will see how we can achieve the same with Azure functions.
You can follow me on Twitter @ThierryCiot