What is Amazon AWS Lambda?
In this blog, we will discuss the concept of AWS Lambda which a serverless compute service offered by the Amazon. You must be wondering when there are so many compute services like Amazon EC2, AWS Elastic beanstalk, AWS OpsWorks, Lightsail etc are available already then what is special with AWS Lambda services?
Here, you will explore what is AWS Lambda, and where should you use it. Let us start our discussion with basic concepts first then we will deep dive to a practical example of Lambda services later.
Introduction to AWS Lambda
AWS Lambda is a compute service which can run your code without provisioning any Infrastructure or servers. People often call Lambda as Lambda Functions. Lambda will RUN your code only when it is triggered. All you need to do is upload your code to AWS or you can write your code in the Lambda in-line editor itself.
You need to pay for the compute time you are consuming. There is no charge whenever your code is not running. We can use AWS Lambda to run your code in response to the events.ie., these events or state changes will be the triggers for the Lambda Functions.
Currently Lambda will support the following languages for writing the code.
What is AWS Lambda? How does it work?
AWS Lambda is a serverless compute service that lets you run code without provisioning any servers with less administrative overhead. All you need to concentrate on the code part for the applications. There is no charge for the code when it is not running.
This Lambda compute service provisions or de-provisions the infrastructure resources of AWS cloud as well. It is one of the easy ways for managing the AWS resources automatically. It also works on instances by launching or terminating them, checking their health, helps in auto-scaling, updates etc.
The code that you wanted to run with the help of AWS lambda is a Lambda function. So, you must be familiar with the fact that function runs only when it is called. So, the event source is an entity here that triggers the Lambda function and helps in execution of task as well.
Learn AWS from the Basic to Advanced Level with Hands-on Training, Placements, and more with
AWS Training in Bangalore
Let us understood the concept in deep with the help of an example below –
Consider you are having an application for the image uploads in the cloud. So, each and every time if you are uploading an image into the cloud, will in-turn generate some events in the cloud. Events are nothing but the state changes of a particular matter. These events which are generated by uploading the images will trigger lambda function to execute the code. Simply the manual task is to write the lambda code in the console and also to define the event source properly.
As you can see in the image given above, the images are uploaded in the source bucket in the form of objects. The image upload action will generate an Event in the cloud. These Events will become the Trigger for the particular Lambda Functions. As you can see the whole process is divided into five major steps, let us understand each of them one by one below –
- The user will upload the image in the form of objects into the Source S3 bucket
- Amazon S3 will generate the Notifications/Events for the uploaded Image object.
- Now, it will send the notification/Events to the Lambda function which is acting as a trigger for the Lambda functions.
- Now, the execution role is managed in Lambda by IAS i.e. identity access management, the key service to manage the permission for AWS resources.
- In the last step, the desired Lambda function is invoked
If the same scenario needs to be managed traditionally with development then you need resources to manage the given tasks –
- Sizing, grouping, or scaling the group of servers
- Working on OS updates
- Security patch management
- Monitoring infrastructure for performance or availability.
This was quite tiring or boring task earlier and quite expensive too. So, the use of a robust compute service like AWS Lambda is quite justified here. This is compatible with Node JS, Python, Java etc so you can upload files in zip format too and define the event source finally. Congrats, you are done.
Lambda: Programming Model
We can write Lambda code in any of the following languages
The core concepts of writing the lambda code is as follows.
Handler is the function which AWS Lambda calls when it is triggered by the cloud
events. The first step in writing the Lambda functions is to define the Handler function.
When the Lambda functions are triggered, AWS Lambda service will call the Handler
to execute the statements of the code. The Cloud events are passed as the parameters by the AWS Lambda to the functions. These parameters(Event data) are processed by the handler and it may also invoke other functions or methods in your code.
The context object:
The context object is the second parameter that will be passed by the AWS
Lambda to our code. We can interact with the AWS Lambda using this context object
AWS Lambda functions contain logging statements. Cloud watch is a monitoring and logging service by AWS. Lambda functions saves these logs to the Cloud watch log system. Log
entries are based on the language that you use in the lambda functions.
The Execution status of the lambda functions will be communicated to AWS Lambda. Based on the type of language you used, either synchronously or asynchronously we can end the execution and the results are sent to the AWS Lambda. There may be errors also during the executions. If you trigger lambda synchronously, results are sent directly to the client.
Get Placement Oriented AWS Training from Industry Experts with our AWS Training in Chennai
When to use Lambda?
If you are a solution architect, you must be trying to figure out when to use Lambda. You have the following options to manage your infrastructure, Amazon EC2, AWS OpsWorks, AWS Elastic Beanstalk, and AWS Lambda. Consider the above example again, why we used Lambda there, not other compute services.
AWS OpsWorks and AWS Elastic Beanstalk majorly required for app deployment but our task is to execute the backend code based on the cloud event triggers, not to create an application. So, EC2 could be another option here. But in the case of EC2, you need to manage everything and it is costly. In the case of Lambda, you don’t have to worry about anything, just paste the code and rest will be managed by the compute service itself.
That’s all for the day. This is clear from the discussion that tasks with AWS Lambda are never tedious and boring but they are quite reasonably priced too. Still, if your requirements don’t fit here then you can try other AWS compute services based on project needs and requirements. We wish you luck for a successful implementation!
Lambda Best Practices
Recommendations and Best Practices:
- Lambda codes should be written in a stateless style.
- Should not have any affinity towards the underlying infrastructure.
- All the Lambda requests should be fulfilled within the life span of the request.
- The persistent state of the output should be stored in S3, DynamoDB or any other storage services.
- Adhere to the programming language principles and best practices
Lambda Use Cases
AWS Lambda is widely used in data processing systems or pipelines. AWS lambda can be triggered directly from other AWS services such as S3 buckets, AWS Kinesis , AWS Dynamo DB,
Cloud watch and so on. Few data processing applications include,
- File processing
- Stream Processing
With the help of Lambda we can build serverless backend systems.
Some of the common backend applications include,
- Mobile Backends
- Web Applications
- IoT Backends
Reference Architecture: Image File Processing
You have created an application, which is hosted in AWS platform. People use your application to upload their photos which in turn is stored in the S3 bucket storage. Now your
application will create a thumbnail of these pictures and displays in the users console.
For this scenario, we can use Lambda functions to create the thumbnail’s automatically.
Lambda functions will be triggered by the S3 bucket events.ie, S3 bucket is the Trigger
source for Lambda functions. Now your lambda function will generate the thumbnail and the thumbnails are stored in Another bucket.
Sample Code :
Reference Architecture: IoT
In this architecture we can see how lambda functions are used as a serverless backend
to process IoT Jobs.
This scenario describes the automatic placement of orders for the replacement of
tractor parts using lambda functions. The tractor IoT sensors will send the field data to
the AWS platform as Streams. These steams are captured by the Amazon Kinesis for data processing. Now Kinesis is the source of trigger for Lambda functions which will detect
the trend patterns to place the order automatically for parts replacement.Sample Code :
Reference Architecture: ETL
we can also use Lambda function to work for the ETL processing tasks.
- Data validation
- Data Sorting
- Data filtering
- Data conversion
Learn AWS Course to Boost your Career with our AWS Online Training
In this architecture of order processing and analytics system, the customer orders are stored in an operational database, DynamoDB. Whenever a new database entry is inserted in to the DynamoDB, the lambda functions are
triggered which will run the data transformation code to transform the data and to load the data in the AWS analytics platform – Redshift. BI tools are used to visualize the analyzed data from the analytics platform.