Skip to main content

AWS Lambda is new powerful tool.

Introduction:
In this post we are going to understand about Lambda function and different features of them. We will look all these things one by one. So before starting to Lambda we have to understand first about Serverless Architecture.
Serverless Architecture:
To understand what AWS Lambda is, we have to first understand what serverless architecture is all about. Serverless applications in general are applications that don’t require any provisioning of servers for them to run.

When you run a serverless application, you get the benefit of not worrying about OS setup, patching, or scaling of servers that you would have to consider when you run your application on a physical server.

Serverless applications or platforms have four characteristics:
1. No server management
2. Flexible scaling
3. No idle capacity
4. High availability

Serverless applications have several components and layers:
• Compute
• API
• Storage
• Inter-process Messaging
• Orchestration
As a cloud provider, AWS offers services that can be used for each of these components that make up a serverless architecture. This is where AWS Lambda comes in.

How Lambda is different than others:
Aws lambda changed our type of doing work. Currently If we want to run any application or run any website, we take one server. We are taking windows or Linux generally. In both of cases we are using virtual machine. So, we are taking basically ec2 instance. EC2 easily available on AWS cloud. After that we deployed our application on that EC2 and application will run accordingly.
What is difference between EC2 and lambda?
So, we know that for running any application we required server. Without server we cannot run any application.

If I say you don’t need any server to your application then what?
Yes, it is possible. You have given one thing on which only you put your code and the application will run. You don’t care about to server and all.
Lambda is one which is very powerful tool for running your application. Lambda can do it for you to run application serverless. You can perform your desire task without any server.

Cost-Effective:
If you take server for running application and deployed your application on server. Aws will charge for time for server is running. If our application only run one hour per day but charge, we have to pay whole day because server is running all day.
But it not case with lambda your application is there for while in lambda. You don’t need pay all time. We have to pay only time for which lambda is running. Sometimes bill is calculated on milliseconds.

Describing AWS Lambda:
AWS Lambda service is a high-scale, provision-free serverless compute offering based on functions. It is used only for the compute layer of a serverless application. The purpose of AWS Lambda is to build event-driven applications that can be triggered by several events in AWS.
In the case where you have multiple simultaneous events, Lambda simply spins up multiple copies of the function to handle the events. In other words, Lambda can be described as a type of function as a service (FaaS). Three components comprise AWS Lambda:
1. A function: This is the actual code that performs the task.
2. A configuration: This specifies how your function is executed.
3. An event source (optional): This is the event that triggers the function. You can trigger with several AWS services or a third-party service.
When you specify an event source, your function is invoked when an event from that source occurs. The diagram below shows what this looks like:

Running a Lambda function:
When configuring a lambda function, you specify which runtime environment you’d like to run your code in. Depending on the language you use, each environment provides its own set of binaries available for use in your code.
You are also allowed to package any libraries or binaries you like as long as you can use them within the runtime environment. All environments are based on Amazon Linux AMI.
The current available runtime environments are:
· NodeJS
· Python
· Go
· Java
· Ruby
· .Net
· C#

When running a lambda function, we only focus on the code because AWS manages capacity and all updates.

Event source:
Although AWS Lambda can be triggered using the Invoke API, the recommended way of triggering lambda is through event sources from within AWS.
There are two models of invocation supported:
(a) Push which get triggered by other events such as API gateway, new object in S3 or Amazon Alexa.
(b) Pull where the lambda function goes and poll an event source for new objects. Examples of such event sources are DynamoDB or Amazon Kinesis.

Lambda configuration:
There are few configuration settings that can be used with lambda functions:
1. Memory dial which controls not just the memory but also affects how much CPU and network resources is allocated to the function.
2. Version/Aliases are used to revert function back to older versions. This is also key in implementing a deployment strategy such as blue/green or separating production from lower environments.
3. IAM Role gives the lambda function permission to interact with other AWS services and APIs.
4. Lambda function permission defines which push model event source is allowed to invoke the lambda function.
5. Network configuration for outbound connectivity. There are two choices:
6. Environment variables for dynamically injecting values that are consumed by code. So, we can use configurable variables after deployment.
7. Timeouts which is the allowed amount of time a function is allowed to run before it is timed out.

Create an AWS Lambda:
There are few ways to create a lambda function in AWS, but the most common way to create it is with the console but this method should only be used if testing in dev. For production, it is best practice to automate the deployment of the lambda.

There are few third-party tools to set up automation, like Terraform, but since we are specifically talking about an AWS service, AWS recommends using Serverless Application Model (SAM) for this task. SAM is pretty much built on top of AWS CloudFormation and the template looks like a normal CloudFormation template except it has a transform block that specifically says we want the template to be a SAM template as opposed to a normal CloudFormation template. You can take a look at some example templates in the AWSlabs.

AWS Lambda use cases:
You can use AWS Lambda in a variety of situations, including but not limited to:
1. Processing of S3 file. We can easily trigger lambda function on object create in s3. So, we can convert or process file as per our requirement or we can transfer file content to other services like SQS or SNS. All processing logic is in lambda function code.
2. Log transfers where a lambda function is invoked every time there is a new log event in CloudWatch to transfer the logs into tools like Elasticsearch and Kibana.
3. A website where you can invoke your Lambda function over HTTP using Amazon API Gateway as the HTTP endpoint.
4. Mobile applications where you can create a Lambda function to process events published by your custom application.
5. Some custom actions are required while deploying our application on ec2. So, we can trigger lambda on ec2 launching and complete our custom actions.


Comments