Amazon Web services(AWS) Lambda-Awsbloginfo

AWS Lambda is a responsive cloud service that inspects actions within the application and responds by deploying the user-defined codes, known as functions. It automatically manages the compute resources across multiple availability zones and scales them when new actions are triggered.

AWS Lambda supports the code written in Java, Python and Node.js, and the service can launch processes in languages supported by Amazon Linux (includes Bash, Go & Ruby).

Following are some recommended tips while using AWS Lambda.

  • Write your Lambda function code in a stateless style.

  • Never declare any function variable outside the scope of the handler.

  • Make sure to have a set of +rx permissions on your files in the uploaded ZIP to ensure Lambda can execute code on your behalf.

  • Delete old Lambda functions when no longer required.

History of Cloud

Lambda

As we know that computer hardware is very heavy, and from decades, number of layers of abstraction have been built in code.

Data Centre: Let's first talk about computer hardware in the Data centre. In this case, someone is needed to check whether it is turned on or not and connected to the network. Provisioning of resources is not very easy as we need to talk to people, we have to call to data centre providers to allow access to the machine. It takes a minimum of 10 days to provide the resources. Suppose you want to place an order for a Database server or web server, this service will be provided after 10 days.

IAAS (Infrastructure As A Service): In 2006, Amazon launched an EC2 instance. You can provision the machine with API calls using a command line or web browser. IAAS service was born, and developers around the world were happy as they do not have to provides the physical services anymore. You can provide the virtual machine anywhere in the world and do whatever you want. IAAS is running in a virtual machine as well as in a physical machine. You have to manage Windows, Linux, or maybe some corruption occurs in a disk due to which your operating system is lost then you need to reinstall the operating system.

PAAS (Platform As A Service): Amazon came out with a new service known as PAAS. You need to upload your code and Amazon will provide the underline resources. You still managing Window, Linux. Amazon would not do for you.

Containers: Containers are isolated and lightweight still they need to be deployed in a server, and you have to keep your container running. All these things that you need to manage.

Lambda: Amazon released Lambda in 2015. You do not have to take care of managing Data centre, managing infrastructure as a service, managing platform as a service or container. You need to upload the code and Amazon will do everything for you.

What is Lambda?

  • Lambda is used to encapsulate Data centres, Hardware, Assembly code/Protocols, high-level languages, operating systems, AWS APIs.
  • Lambda is a compute service where you can upload your code and create the Lambda function.
  • Lambda takes care of provisioning and managing the servers used to run the code.
  • While using Lambda, you don't have to worry about scaling, patching, operating systems, etc.

Lambda can be used in the following ways:

  • It can be used as an event-driven compute service where AWS Lambda runs your code in response to events. These events could be changes to data in an Amazon S3 bucket or an Amazon DynamoDB table.
  • It can be used as a compute service to run your code in response to HTTP requests using Amazon API calls made using AWS SDKs.

How does Lambda work

Lambda
  • User uploads an image to S3.
  • S3 triggers an event, and this event is a Lambda function.
  • Lambda function takes this image, and then encode the image. When an image is encoded, it gets stored in S3.
  • The Lambda function might trigger other Lambda event which is returning image location back to the user.
  • The Lambda might trigger another Lambda event that takes the image from the S3 bucket and stores it in another S3 bucket located anywhere in the world.

Conclusion

  • From the above example, we conclude that Lambda event can trigger another Lambda event and they can communicate with other AWS resources.
  • An important feature of Lambda is that it scales automatically. While using Lambda, you do not have to maintain Elastic Load Balancer. It either scales up or scales out based on the condition. Scaling up means increasing the resources in terms of RAM such as 8 GB to 12 GB. Scaling out means adding more and more instances. For example, Load Balancer. If load increases, you scale out rather than scaling up the instances. Lambda function scales out automatically. For example, if we have a two-line function, and millions of users hit the same function, millions of functions created to serve the millions of users.

How to Configure AWS Lambda?

Follow these steps to configure AWS Lambda for the first time.

Step 1 − Sign in to AWS account.

Step 2 − Select Lambda from AWS services section.

Amazon Web Services

Step 3 − Select a Blueprint (optional) and click the Skip button.

Lambda

Step 4 − Provide the necessary details to create a Lambda function as shown in the following screenshot and paste the Node.js code that will be triggered automatically whenever a new item is added in DynamoDB. Select all the required permissions.

Configure FunctionLambda Function Handler

Step 5 − Click the Next button and verify your details.

Step 6 − Click the Create Function button.

Now, when we select the Lambda service and select the Event Sources tab, there will be no records. Add at least one source to the Lambda function to work. Here, we are adding DynamoDB Table to it.

We have created a table using DynamoDB (we will discuss this in detail in DynamoDB chapter).

Step 7 − Select the stream tab and associate it with the Lambda function.

Amazon DynamoDB

You will see this entry in Event Sources Tab of Lambda Service page.

Step 8 − Add some entries into the table. When the entry gets added and saved, then Lambda service should trigger the function. It can be verified using the Lambda logs.

Step 9 − To view logs, select the Lambda service and click the Monitoring tab. Then click the View Logs in CloudWatch.

Monitoring

Benefits of AWS Lambda

Following are some of the benefits of using Lambda tasks −

  • Lambda tasks need not to be registered like Amazon SWF activity types.

  • We can use any existing Lambda functions that you’ve already defined in workflows.

  • Lambda functions are called directly by Amazon SWF; there is no need design a program to implement and execute them.

  • Lambda provides us the metrics and logs for tracking function executions.

AWS Lambda Limits

Following are the three types of Lambda limits.

THROTTLE LIMIT

The throttle limit is 100 concurrent Lambda function executions per account and is applied to the total concurrent executions across all functions within a same region.

The formula to calculate the number of concurrent executions for a function = (average duration of the function execution) X (number of requests or events processed by AWS Lambda).

When throttle limit is reached, then it returns a throttling error having an error code 429. After 15-30 minute you can start work again. The throttle limit can be increased by contacting AWS support center.

RESOURCES LIMIT

The following table shows the list of resources limits for a Lambda function.

ResourceDefault Limit
Ephemeral disk capacity ("/tmp" space)512 MB
Number of file descriptors1,024
Number of processes and threads (combined total)1,024
Maximum execution duration per request300 seconds
Invoke request body payload size6 MB
Invoke response body payload size6 MB

SERVICE LIMIT

The following table shows the list of services limits for deploying a Lambda function.

ItemDefault Limit
Lambda function deployment package size (.zip/.jar file)50 MB
Size of code/dependencies that you can zip into a deployment package (uncompressed zip/jar size)250 MB
Total size of all the deployment packages that can be uploaded per region1.5 GB
Number of unique event sources of the Scheduled Event source type per account50
Number of unique Lambda functions you can connect to each Scheduled Event5

For latest updated limit structure & more information, visit − https://docs.aws.amazon.com/lambda/latest/dg/limits.html/

Comments

Popular posts from this blog

Relational Database-Awsbloginfo

AWS Blog Info - Content

Introduction To Amazon Web services-AWS Blog Info