Let’s learn the fundamentals of AWS Lambda and how to run a function step by step.
Introduction
When you are building applications, you want them to deliver an excellent user experience. To make the magic happen, your application needs a backend code that runs in response to events.
But managing the infrastructure to host and execute backend code requires you to size, provision, and scale a bunch of servers, manage operating system updates, apply security patches and then monitor all this infrastructure for performance and availability.
Wouldn’t it be nice if you could focus on building great applications without having to worry about their infrastructure? That is where AWS Lambda comes into the picture.
What is AWS Lambda?
AWS Lambda is a serverless compute service that lets you run your code without worrying about provisioning or managing any server. You can run your application or backend service using AWS Lambda with zero administration. Just upload your code on Lambda, and it will run your code, even scale the infrastructure with high availability.
The code which you run on AWS Lambda is called a lambda function. Currently, it supports the following programming languages:
- Java
- Python
- C#
- Node.js
- Go
- PowerShell
- Ruby
It also provides a runtime API that can be used to run functions written in other (native) programming languages.
To work with AWS Lambda, there is only one prerequisite; you should have an account on AWS from where you can access the AWS management console.
You can call Lambda is FaaS (Function-as-a-Service) by AWS.
AWS Lambda Features
Below are some of the important features offered by AWS Lambda:
- AWS Lambda easily scales the infrastructure without any additional configuration. It reduces the operational work involved.
- It offers multiple options like AWS S3, CloudWatch, DynamoDB, API Gateway, Kinesis, CodeCommit, and many more to trigger an event.
- You don’t need to invest upfront. You pay only for the memory used by the lambda function and minimal cost on the number of requests hence cost-efficient.
- AWS Lambda is secure. It uses AWS IAM to define all the roles and security policies.
- It offers fault tolerance for both services running the code and the function. You do not have to worry about the application down.
AWS Lambda Pricing
AWS Lambda pricing depends on the duration and the memory used by the lambda function written by you. The maximum you can assign is 3008 MB memory to a lambda function in 64 MB increments. Below is a pricing table with all the memory slabs for 100 milliseconds.
How AWS Lambda Works?
- First, you create a function and add basic information to it, like the programming language to be used in the function.
- Then you write your code on the lambda editor or upload it in a supported programming language in a zip file.
- Once the lambda code is uploaded, the service handles all the capacity scaling, patching, and administration of the infrastructure.
- To run the code, you need to trigger the lambda function with an external AWS service, which can invoke the lambda function. For example, it can be an S3 bucket.
- Within a few seconds, lambda will be ready to trigger your function automatically when an event occurs. AWS Lambda runs your code when the trigger event is called. It provisions manages and monitors the servers for you.
- If your function requires a lot of processing power, it will choose an instance type that has more processing power and RAM, or else if your lambda code only executes for two seconds, it will select the lowest possible instance, which saves your money and time.
So, that is how AWS Lambda works internally. Let me show you a demo on AWS Lambda.
Creating AWS Lambda Function
I am going to create a very simple game using the lambda function in Node.js for this article. I will create a lambda function for rolling a dice, generating a number randomly between 1 to 6, and printing it.
- Go to AWS management console, and in search bar type Lambda, click on Lambda.
- Function window will appear, click on Create function.
- You will get different options for creating a function with their explanation. Since I am creating it from scratch, I will select Author from Scratch.
- After that, you need to fill some necessary information for this lambda function. Enter the function name and select the Node.js version, which you want to use for this function.
- You also need to choose an execution role. Since I donโt have any existing role defined in my AWS account, I will go ahead and select create a new role option. Click on Create function.
- You will get a success message that the unction got created. Click on the Designer window to minimize it.
- Next will be the Function code window.
- Put the code mentioned below in the editor. You can also upload the code using the zip file, but I am using the internal AWS code editor.
- It is a simple code which takes number only from 1 to 6 and uses a random math function to generate a number randomly and print it when the function is called.
exports.handler = async (event) => {
const min = 1;
const max = 6;
const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
const out = 'Dice throw result is: ' + randomNum;
return out;
};
- Now click on the Test button at the top right corner. You will get a pop up to configure a test event, enter an event name, and click on configure.
- Now click on Save and then Test.
In the Execution Result, you will see the output of the function logic we just wrote. It prints โ Dice throw result is 2.
- Scroll up and click on detailed execution results to get the complete summary of this lambda function with the expected output. Details such as to request-id, duration, billed duration, resources configured, etc. with log output are also available.
- Click on the monitoring tab to visualize the cloud watch logs and lambda function performance over a defined time.
- If you get inside the logs which got create by CloudWatch, you can look at the details of what happened when the lambda function ran, which was monitored by CloudWatch.
Conclusion
Getting started with Lambda is really easy. If your business application requires to run backend code, you might consider using a serverless platform like AWS Lambda.