AWS Lambda

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.

  •  Node.js
  •  Java,
  •  C#
  •  Go
  •  Python

What is AWS Lambda? How does it work?

how lambda works

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.

Click Here! Get Prepared for Interviews!

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.

key product feature of aws lambda

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 –

  1. The user will upload the image in the form of objects into the Source S3 bucket
  2. Amazon S3 will generate the Notifications/Events for the uploaded Image object.
  3. Now, it will send the notification/Events to the Lambda function which is acting as a trigger for the Lambda functions.
  4. 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.
  5. 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

  • Node.js
  • Java
  • C#
  • Go
  • Python

The core concepts of writing the lambda code is as follows.

Handler:

  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

parameters.

Logging:

  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.

Exceptions:

 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.

Click Here! Get AWS 100% Practical Training!

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

DATA Processing

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
  • ETL

Backends

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

aws lambda - 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.

Click Here! To Get AWS Certification Training!

Sample Code :

https://github.com/aws-samples/lambda-refarch-fileprocessing

Reference Architecture: IoT

reference architecture 1

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 :

https://github.com/aws-samples/lambda-refarch-iotbackend

Reference Architecture: ETL

architecture of etl

we can also use Lambda function to work for the ETL processing tasks.
such as.,
– Data validation
– Data Sorting
– Data filtering
– Data conversion

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.

Related Blogs

 

 

Besant Technologies – Chennai & Bangalore Branch Locations

Besant Technologies - Velachery Branch
Plot No. 119, No.8, 11th Main road, Vijaya nagar,
Velachery, Chennai - 600 042
Tamil Nadu, India
Landmark - Reliance Digital Opposite Street
+91-996 252 8294 / 996 252 8293

Besant Technologies - Tambaram Branch
No.2A, 1st Floor, Duraisami Reddy Street,
West Tambaram, Chennai - 600 045
Tamil Nadu, India
Landmark - Near By Passport Seva
+91-996 250 4283

Besant Technologies - OMR Branch
No. 5/318, 2nd Floor, Sri Sowdeswari Nagar,
OMR, Okkiyam Thoraipakkam, Chennai - 600 097
Tamil Nadu, India
Landmark - Behind Okkiyampet Bus Stop,
+91-887 038 4333

Besant Technologies - Porur Branch
No. 180/84, 1st Floor, Karnataka Bank Building
Trunk Road, Porur,
Chennai - 600 116
Tamilnadu, India
Land Mark: Opposite to Gopalakrishna Theatre
+91-755 022 6990

Besant Technologies - Anna Nagar Branch
Plot No:1371, 28th street kambar colony,
I Block, Anna Nagar ,
Chennai - 600 040
Tamil Nadu, India
Landmark - Behind Reliance Fresh
+91-938 404 0493

Besant Technologies - T.Nagar Branch
Old No:146/2- New No: 48,
Habibullah Road,T.Nagar,
Chennai - 600 017
Tamil Nadu, India
Landmark - Opposite to SGS Sabha
+91-984 025 8377

Besant Technologies - Thiruvanmiyur Branch
22/67, 1st Floor,
North mada street, Kamaraj Nagar
Thiruvanmiyur, Chennai 600041
Tamil Nadu, India
Landmark - Above Thiruvanmiyur ICICI Bank
+91-938 404 0493

Besant Technologies - Siruseri Branch
No. 4/76, Ambedkar Street,
OMR Road, Egatoor, Navallur,
Siruseri, Chennai 600130
Tamil Nadu, India
Landmark - Near Navallur Toll Gate, Next to Yamaha Showroom
+91-7338 810 550

Besant Technologies - Maraimalai Nagar Branch
No.37, Ground Floor,
Thiruvalluvar Salai,
Maraimalai Nagar,Chennai 603209
Tamil Nadu, India
Landmark - Near to Maraimalai Nagar Arch
+91-979 123 7373

Besant Technologies - BTM Layout Branch
No 2, Ground floor,
29th Main Road, Kuvempu Nagar,BTM Layout 2nd Coming from Silkboard, take left at AXA company signal,
Stage, Bangalore - 560 076
Karnataka, India
Landmark - Next to OI Play School
+91-762 494 1772 / 74

Besant Technologies - Marathahalli Branch
No. 43/2, 2nd Floor, VMR Arcade,
Varthur Main Road, Silver Springs Layout,
Munnekollal, Marathahalli, Bengaluru - 560037
Karnataka, India
Landmark - Near Kundalahalli Gate Signal
+91-910 812 6341

Besant Technologies - Rajaji Nagar Branch
No. 309/43, JRS Ecstasy, First Floor,
59th Cross, 3rd Block, Bashyam Circle,
Rajaji Nagar, Bangalore - 560 010
Karnataka, India
Landmark - Near Bashyam Circle
+91- 734 916 0004

Besant Technologies - Jaya Nagar Branch
No. 2nd Floor,1575,11th Main Road,
4th T-Block, Pattabhirama Nagar, Jaya Nagar,
Bangalore-560041
karnataka, India
Land Mark: Opposite to Shanthi Nursing Home
+91-733 783 7626

Besant Technologies - Kalyan Nagar Branch
No.513, 4th Cross Rd
2nd Block, HRBR Layout, Kalyan Nagar,
Bangalore-560043
karnataka, India
Land Mark: Opposite to kalayan nagar Axis Bank
+91-938 404 0495

Besant Technologies - Electronic City Branch
No. 7, 3rd Floor, Ganga Enclave,
Neeladri Road, Karuna Nagar, Doddathoguru Village,
Electronics City Phase 1, Electronic City,
Bangalore - 560100, karnataka, India
Land Mark: Adjacent to HDFC Bank and State Bank Of India
+91-782 392 2180

Besant Technologies - Indira Nagar Branch
No.54, 1st Floor,
5th Main kodihalli,
Bengaluru, Karnataka 560008, India
Landmark: Behind Leela Palace Hotel,
+91-910 812 6339

Besant Technologies - HSR Layout Branch
Plot No. 2799 & 2800, 27th Main,
1st Sector, HSR Layout,
Bengaluru, Karnataka 560102, India
+91-733 783 7626