top
upGrad KnowledgeHut SkillFest Sale!

Search

AWS Tutorials

The AWS Lambda is a cloud service provided by Amazon, which is also known as ‘Lambda’ in short. It is a part of the Amazon Web Services and is also referred to as ‘serverless compute’. The word ‘serverless compute’ basically means the service doesn't explicitly require a server to run the code which is supplied by the developers. It runs without the interference from the developer in regard to managing the servers.The word ‘serverless’ might be misleading at times: - This doesn’t mean no server is required to run the code at all, it just means the developer need not worry about facilitating or managing the server. They could simply focus on writing high-quality, bug-free, well thought-about code, and all other requirements (such as providing infrastructure, resources, scaling, storage, server) required to successfully execute the code will be automatically taken care of by the Amazon Lambda service.Serverless also means the server doesn’t need to be running at all points in time. For example- When no request comes in, the server need not be up and running. This way, compute resources are not wasted. The server appears when required and disappears when it completes its work.The developers just write their code and upload it on the Lambda service which takes care of all the requirements that are essential in running the code. This means the gruesome task of deciding the storage or selecting resources which are required to run the code are eliminated from the developer’s behalf.Lambda FunctionThe code which developers write and is executed inside the Lambda service is known as ‘Lambda function’. It is trivial sense to know that a function can be executed only when it is called. This call could be a trigger or a simple function call or a HTTP request or occurrence of an event (external or internal) or a new document which is uploaded to S3, a scheduled job or a simple notification which would lead to the Lambda function being called.Salient features of AWS LambdaAllows developers to focus on writing efficient and high-quality code.Manages other AWS resources which are required to execute the code written by developers successfully.Lambda takes care of initiating as well as terminating instances.Lambda also takes care of scaling the resources up and down (it is elastic- which means it can be easily scaled up).Lambda frequently provides software patches and updates which are automatically handled by the service itself.Users/developers pay only for the resources they consume and the requests they make to the Lambda function.There are 2 main characteristics of Lambda:It is event-drivenIt is stateless and serverlessNow let us see how each of these characteristics contributes to the behaviour of Lambda. Event drivenThis means the emphasis is on executing the code that is provided to it. This code is executed in the most efficient way possible by automatically providing the resources needed for it.  ServerlessThe developer doesn’t need to consider providing or managing the servers needed to run their code. They don’t need to worry about the storage or resources required as well.  At one point in time, the Lambda Services could potentially replace one of Amazon's own core services- Virtual Machines. This is not far, but for now, we will only be looking at how Lambda has made a difference to the world of cloud computing.  It provides abstract server management, because of which the developers who write code only have to focus their attention on efficient programming and execution, rather than worrying about what resources are needed or the time complexity or space complexity. As and when new tasks are introduced in the code, the Lambda service works on scaling up the resources and the storage. As and when the code is reduced, Lambda works on scaling down the resources and compute power.   Stateless This means the output of the function written inside the Lambda service depends only on the parameters which are passed to the method and nothing else.  Language SupportThe developers can program in different languages, which includes Python, Java, Node.js, Go and C#.  In addition to support for these programming languages, compiler tools like Maven, Gradle and function building packages also can be used.  What kind of code is written and executed with Lambda?Lambda service is primarily used as a trigger to execute other programs from AWS services periodically.  For example: Suppose there is a requirement to fetch data from different sources on a daily basis, the data can be loaded into a database by triggering a program/code that would be written inside the Lambda service. This Lambda service can be programmed to trigger at certain times of the day.  The code executed in Lambda could also be a response to other web services in Amazon, such as creating an object in Amazon Store Service (S3) bucket.  How to code inside AWS Lambda?A handler is considered to the point of entry inside a Lambda service. Usually, the data to a handler is in JSON format. As trivial as this sounds, the output is also in the JSON format.  The runtime environment needs to be specified, for example, if the language in which the developer codes is Python, a Python runtime environment needs to be provided.  Write the code in a stateless fashion, i.e. the output depends on the parameters passed to the method which is executed inside the Lambda service.  All the method variables need to be declared inside the handler.  For Lambda service to execute code based on a certain condition, it should have the ‘+rx’ (read and execute) permissions.  Delete methods which are not being used by the Lambda service.  The pricing of AWS Lambda It is a pay-as-you-use service, which means the user pays for the resources they consume, and nothing else. The charges can be based on the number of requests which are made to the Lambda function as well as based on the time for which the code gets executed.  A request is counted when the function starts its execution of the code when it is called or it is triggered by other external/internal events. The first one million requests are not charged and every other request is charged according to $0.20 per request.  The time for the code to get executed is considered from the moment the code begins its execution and ends when the function is terminated or completes its execution. This pricing is based on the amount of memory which has been allocated to the Lambda function.  Every time a request is sent from the Lambda service to the server internally, it is charged, i.e it is charged based on the number of requests sent to the server and the requests which are potentially answered back. The pricing also depends on the amount of compute time required to run the code written inside the Lambda service.  They have a free tier which can be used as a sample to understand how Lambda could be used in conjunction with AWS EC2 instance, CloudWatch and other Amazon Web Services.  ExampleConsider the below example which is executed on Lambda: 1. The input is in JSON format. {  “num_1” : 40.0,  “num_2”: 100.0  } 2. Suppose our end result needs to be the difference, remainder and sum of the inputs. Observe that the output is also in JSON format.  {  “num_1” : 40.0,  “num_2”: 100.0,  “sum”: 140.0,  “difference”:60,  “remainder”:2.5  } 3. Sign up for a free trial of the AWS by creating or signing into your account.4. We will be using Python as our coding language to execute methods within Lambda. Since Python is a scripting language, it can be coded within the AWS console.  5. Go to the ‘Lambda’-> ‘Functions’->’Create Function’ as in the below screenshot: 6. Click on the ‘Author from scratch’ and enter all the basic information required as in the below screenshot:7. Don’t forget to enter the runtime. Here, we are using ‘Python 3.7’.8. Now click on ‘Create Function’ to create a new function.  9. Click on ‘Select a test event’ and select ‘Configure test events’.10. Create a new test event, provide a name to the Event name and enter the inputs mentioned in step 1.   Now, click on ‘Create’. It can be observed that the Event Template has been filled in prior to us entering anything (‘Hello World’).   Instead of this, we can enter the data which we specified in step 2 to see the difference, remainder and sum of the two inputs.11. This event can be tested by clicking on the ‘Test button’. The output would be ‘Hello from Lambda!’ 12. The default output (Hello from Lambda!) can be seen in the below snip. Also, it is in the JSON format.The above screenshot, the “lambda_function.py” is as follows:The execution result can be seen above is:Note: Third party logging APIs is supported by Lambda, and this can be achieved with the help of Amazon API Gateway Service. Design specifications of AWS LambdaThese can’t be called as limitations, since they have been designed to be what they are. Such boundaries have been discussed below: Hardware limitations, such as disk size being limited to 512 MB. The execution time out can be 5 minutes maximum.  Data sent through a ‘GET’ or ‘PUT’ request can’t be more than 6MB.  The size of type of request, headers, and other metadata can’t be more than 128 KB.  Conclusion In this post, we saw the significance of AWS Lambda and how it can be used to execute code without the user worrying about the resources, storage and infrastructure. We also saw the pricing specifications, in addition to the design boundaries of AWS Lambda.
logo

AWS Tutorials

How to execute code with Amazon Web Services Lambda?

The AWS Lambda is a cloud service provided by Amazon, which is also known as ‘Lambda’ in short. It is a part of the Amazon Web Services and is also referred to as ‘serverless compute’. The word ‘serverless compute’ basically means the service doesn't explicitly require a server to run the code which is supplied by the developers. It runs without the interference from the developer in regard to managing the servers.

The word ‘serverless’ might be misleading at times: - This doesn’t mean no server is required to run the code at all, it just means the developer need not worry about facilitating or managing the server. They could simply focus on writing high-quality, bug-free, well thought-about code, and all other requirements (such as providing infrastructure, resources, scaling, storage, server) required to successfully execute the code will be automatically taken care of by the Amazon Lambda service.

Serverless also means the server doesn’t need to be running at all points in time. For example- When no request comes in, the server need not be up and running. This way, compute resources are not wasted. The server appears when required and disappears when it completes its work.

The developers just write their code and upload it on the Lambda service which takes care of all the requirements that are essential in running the code. This means the gruesome task of deciding the storage or selecting resources which are required to run the code are eliminated from the developer’s behalf.

Lambda Function

The code which developers write and is executed inside the Lambda service is known as ‘Lambda function’. It is trivial sense to know that a function can be executed only when it is called. This call could be a trigger or a simple function call or a HTTP request or occurrence of an event (external or internal) or a new document which is uploaded to S3, a scheduled job or a simple notification which would lead to the Lambda function being called.

Salient features of AWS Lambda

  • Allows developers to focus on writing efficient and high-quality code.
  • Manages other AWS resources which are required to execute the code written by developers successfully.
  • Lambda takes care of initiating as well as terminating instances.
  • Lambda also takes care of scaling the resources up and down (it is elastic- which means it can be easily scaled up).
  • Lambda frequently provides software patches and updates which are automatically handled by the service itself.
  • Users/developers pay only for the resources they consume and the requests they make to the Lambda function.

There are 2 main characteristics of Lambda:

  • It is event-driven
  • It is stateless and serverless

Now let us see how each of these characteristics contributes to the behaviour of Lambda. 

Event driven

This means the emphasis is on executing the code that is provided to it. This code is executed in the most efficient way possible by automatically providing the resources needed for it.  

Serverless

The developer doesn’t need to consider providing or managing the servers needed to run their code. They don’t need to worry about the storage or resources required as well.  

At one point in time, the Lambda Services could potentially replace one of Amazon's own core services- Virtual Machines. This is not far, but for now, we will only be looking at how Lambda has made a difference to the world of cloud computing.  

It provides abstract server management, because of which the developers who write code only have to focus their attention on efficient programming and execution, rather than worrying about what resources are needed or the time complexity or space complexity. As and when new tasks are introduced in the code, the Lambda service works on scaling up the resources and the storage. As and when the code is reduced, Lambda works on scaling down the resources and compute power.   

Stateless

This means the output of the function written inside the Lambda service depends only on the parameters which are passed to the method and nothing else.  

Language Support

The developers can program in different languages, which includes Python, Java, Node.js, Go and C#.  

In addition to support for these programming languages, compiler tools like Maven, Gradle and function building packages also can be used.  

What kind of code is written and executed with Lambda?

Lambda service is primarily used as a trigger to execute other programs from AWS services periodically.  

For example: Suppose there is a requirement to fetch data from different sources on a daily basis, the data can be loaded into a database by triggering a program/code that would be written inside the Lambda service. This Lambda service can be programmed to trigger at certain times of the day.  

The code executed in Lambda could also be a response to other web services in Amazon, such as creating an object in Amazon Store Service (S3) bucket.  

How to code inside AWS Lambda?

  • A handler is considered to the point of entry inside a Lambda service. Usually, the data to a handler is in JSON format. As trivial as this sounds, the output is also in the JSON format.  
  • The runtime environment needs to be specified, for example, if the language in which the developer codes is Python, a Python runtime environment needs to be provided.  
  • Write the code in a stateless fashion, i.e. the output depends on the parameters passed to the method which is executed inside the Lambda service.  
  • All the method variables need to be declared inside the handler.  
  • For Lambda service to execute code based on a certain condition, it should have the ‘+rx’ (read and execute) permissions.  
  • Delete methods which are not being used by the Lambda service.  

The pricing of AWS Lambda

It is a pay-as-you-use service, which means the user pays for the resources they consume, and nothing else. The charges can be based on the number of requests which are made to the Lambda function as well as based on the time for which the code gets executed.  

A request is counted when the function starts its execution of the code when it is called or it is triggered by other external/internal events. The first one million requests are not charged and every other request is charged according to $0.20 per request.  

The time for the code to get executed is considered from the moment the code begins its execution and ends when the function is terminated or completes its execution. This pricing is based on the amount of memory which has been allocated to the Lambda function.  

Every time a request is sent from the Lambda service to the server internally, it is charged, i.e it is charged based on the number of requests sent to the server and the requests which are potentially answered back. The pricing also depends on the amount of compute time required to run the code written inside the Lambda service.  

They have a free tier which can be used as a sample to understand how Lambda could be used in conjunction with AWS EC2 instance, CloudWatch and other Amazon Web Services.  

Example

Consider the below example which is executed on Lambda: 

1. The input is in JSON format. 

{ 
“num_1” : 40.0, 
“num_2”: 100.0 
} 

2. Suppose our end result needs to be the difference, remainder and sum of the inputs. Observe that the output is also in JSON format.  

{ 
“num_1” : 40.0, 
“num_2”: 100.0, 
“sum”: 140.0, 
“difference”:60, 
“remainder”:2.5 
} 

3. Sign up for a free trial of the AWS by creating or signing into your account.

4. We will be using Python as our coding language to execute methods within Lambda. Since Python is a scripting language, it can be coded within the AWS console.  

5. Go to the ‘Lambda’-> ‘Functions’->’Create Function’ as in the below screenshot: 

Amazon Web Services Lambda

6. Click on the ‘Author from scratch’ and enter all the basic information required as in the below screenshot:

Amazon Web Services Lambda

7. Don’t forget to enter the runtime. Here, we are using ‘Python 3.7’.

8. Now click on ‘Create Function’ to create a new function.  

9. Click on ‘Select a test event’ and select ‘Configure test events’.

Amazon Web Services Lambda

10. Create a new test event, provide a name to the Event name and enter the inputs mentioned in step 1.  
 Now, click on ‘Create’. It can be observed that the Event Template has been filled in prior to us entering anything (‘Hello World’).  
 Instead of this, we can enter the data which we specified in step 2 to see the difference, remainder and sum of the two inputs.

Amazon Web Services Lambda

11. This event can be tested by clicking on the ‘Test button’. The output would be ‘Hello from Lambda!’ 

Amazon Web Services Lambda

12. The default output (Hello from Lambda!) can be seen in the below snip. Also, it is in the JSON format.

Amazon Web Services Lambda

The above screenshot, the “lambda_function.py” is as follows:

Amazon Web Services Lambda

The execution result can be seen above is:

Amazon Web Services Lambda

Note: Third party logging APIs is supported by Lambda, and this can be achieved with the help of Amazon API Gateway Service. 

Design specifications of AWS Lambda

These can’t be called as limitations, since they have been designed to be what they are. Such boundaries have been discussed below: 

  • Hardware limitations, such as disk size being limited to 512 MB. 
  • The execution time out can be 5 minutes maximum.  
  • Data sent through a ‘GET’ or ‘PUT’ request can’t be more than 6MB.  
  • The size of type of request, headers, and other metadata can’t be more than 128 KB.  

Conclusion 

In this post, we saw the significance of AWS Lambda and how it can be used to execute code without the user worrying about the resources, storage and infrastructure. We also saw the pricing specifications, in addition to the design boundaries of AWS Lambda.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

tenzin nyima

Whoever has contributed to this article...I would like to say thank you... it has been of good help to the readers.

alvi

This blog is very helpful and informative, and I really learned a lot from it.

alvi

It is very helpful and very informative, and I really learned a lot from this article.

alvi

Such a very useful article. I would like to thank you for the efforts you made in writing this awesome blog.

Jeanne

Very useful and awesome blog!