Transform Your Cybersecurity Career with Our Latest Release – PCNSE: Palo Alto Certified Network Security Engineer. Enroll in PCNSE Course Today!

What is AWS Lambda?

Recent Posts

Share this post:

Table of Contents

AWS Lambda

In this blog, we will discuss the basics of AWS Lambda and how to use it. But before you start learning about AWS Lambda, you need to understand what is Serverless.

In the past, almost all IT administrators had to worry about the provisioning and management of the organization’s servers. The IT people were desperately in need of something that could help in lessening their administrative load. In 2006, the serverless concept came into being and became popular instantly.

Serverless computing is a cloud-based execution model, a form of utility computing, where the cloud service providers act as the server and manage the allocation of resources dynamically. It is also called “Function-as-a-Service” (FaaS).

A serverless platform or service provides you the following:

  • No server management
  • Flexible scaling
  • High availability
  • No idle capacity

The AWS platform is the leader of the cloud and serverless architecture; it provides many services for serverless computation.

AWS Lambda removes the need for conventional computing resources and thus reduces operational costs. There are many benefits such as rapid development, simpler financial management, scaling and operating costs reduction.

In fact, Lambda takes care of this if you regularly alter your memory use. The model is based on a used memory, the number of requests, and execution time rounded up to the nearest 100 milliseconds. It has a “Pay as You Go” model.

AWS Lambda is a compute service to provide high scale and provision-free computation using functions. You can upload your code to create a Lambda function. These functions can be triggered by events enabling you to build event-driven reactive systems. Lambda can be used in various scenarios such as a Lambda function can be triggered when there are changes occurring in your data, let’s say, in an AWS DynamoDB table, or in your S3 bucket. It can also be used in response to HTTP requests by using the Amazon API Gateway. 

When there are multiple events to respond to, Lambda runs copies of your function in parallel to provide scaling by the size of the workload. Architects use the Lambda function to reduce wasted capacity.

Lambda is event-driven, so it can be described as FaaS, which goes about building event-driven compute systems by using functions as the unit of deployment and execution. Serverless FaaS is the type of FaaS in which the vendor is responsible for providing provision-free scalability and no virtual machines are present in the programming model. You can run Lambda functions for any application; it will scale your code with high availability.

Portions of each Lambda function are: codes that you want to execute, the configuration that defines how the code will execute and event sources that detect the events and invoke your function. We will discuss these terms in detail, later. You do not need to write the code to integrate event sources with your Lambda function, neither are you responsible for managing the infrastructure that detects the events and enables your function. Also, you are not responsible for scaling your Lambda function. You should only worry about your application logic and configuration of event sources to make your code run.

Following is an illustration of simplified running Lambda function architecture:

When you configure an event source for your function, and that event occurs, your code will be invoked. Your Lambda function code can execute any logic that your application requires. It could be business logic, it can reach out to external web servers, or it can integrate with other AWS services. Lambda function can trigger other Lambda functions.

Lambda Functions

The code you run on AWS Lambda is called a “Lambda function”. Fundamentally, Lambda is used to executing codes. This code could be of any language that is supported by Lambda (C#, Go, Java, Node.js, and Python), it is also allowed to bring libraries, artifacts or compiled native binaries that can be executed as part of your function code package. The code that you have written in any other languages can also be executed by invoking that code by any of the supported languages in the Lambda runtime environment. The Lambda apps are “stateless” and have no connection with the infrastructure behind them so that Lambda can quickly start as many copies of the function as required for the inbound events.

You can use specific AWS services (e.g., a particular Amazon S3 pool, Amazon DynamoDB table, Amazon Kinesis , or Amazon SNS notifications) after uploading the code to AWS Lambda. Then Lambda executes the functions when resources change and handles the machine resources to meet incoming requests.

What are the Benefits of using AWS Lambda?

These are the following benefits of using AWS Lambda

  • Continuous scaling
  • Faster development
  • Reduction in operational costs
  • Sub second metering

Scenario

A company wants to store information about the notification of messages that are sent to inform the clients about their company product in the database, and they want the solution by using Compute resources in AWS Cloud in a free tier. Also, they have some other requirements:

  1. The database must be DynamoDB
  2. It must create a function in AWS
  3. It must create its own role for permission

Solution

With the help of Lambda function, SNS and DynamoDB you can meet the above requirements.

Step-by-Step Guide

Step no. 1: Create a topic the same way we created in Lab 8.2. Now go to “DynamoDB” under “Database”.
Step no. 2: Click on “Create Table”.
Step no. 3: Enter the table name and add partition key and sort key.
Step no. 4: Your table has been created.
Step no. 5: Go to “Lambda” under “Compute”.
Step no. 6: Click on “Create Function”.
Step no. 7: Go to “Blueprints” and search SNS. Here you will find “sns message”. Click on it.
Step no. 8: Enter basic details and select an existing role.
Step no. 9: The role is made by creating the policy first. In policy, you must define that read-write has access to Dynamo DB whenever the Lambda function invokes due to SNS notification.
{
    “Version”: “2012-10-17”,
    “Statement”: [
        {
            “Sid”: “Stmt1428510662000”,
            “Effect”: “Allow”,
            “Action”: [
                “dynamodb:*”
            ],
            “Resource”: [
                “<arn of table>”
            ]
        }
    ]
}
ARN of the table is taken from here (shown in figure).
Step no. 10: Enter details and click on “Create Policy”.
Step no. 11: Your policy has been created. Go to “Roles” and create the role.
Step no. 12: Select the service, here we will select Lambda, then go to “Next Permission” and select the policy that we created, click on “Review”. Now enter details and click “Create Roles”.
This way, we created the role “Lambda”; Now, we will use an existing role in Lambda Function.
Step no. 13: Scroll down Lambda Window and select “Enable trigger”. Enter SNS topic, and ARN as well.
Step no. 14: Click on “Create Function”, we will change the code of the function later.
Step no. 15: Now your function is created. Scroll down the page and go to “Function code” and write the code for your desired function.
Our function to trigger Lambda function through SNS notification is here:
console.log(‘Loading event’);
var aws = require(‘aws-sdk’);
var ddb = new aws.DynamoDB({params: {TableName: ‘Lambdatable’}});
 
exports.handler = function(event, context) {
  var SnsMessageId = event.Records[0].Sns.MessageId;
  var SnsPublishTime = event.Records[0].Sns.Timestamp;
  var SnsTopicArn = event.Records[0].Sns.TopicArn;
  var LambdaReceiveTime = new Date().toString();
  var itemParams = {Item: {SnsTopicArn: {S: SnsTopicArn},
  SnsPublishTime: {S: SnsPublishTime}, SnsMessageId: {S: SnsMessageId},
  LambdaReceiveTime: {S: LambdaReceiveTime}  }};
  ddb.putItem(itemParams, function() {
    context.done(null,”);
  });
};
Step no. 16: Now click on “Save”, then click on “Test” and a pop-up window will appear to create an event.
Select the “Event template” of SNS and enter the name of the event. Click on “Create”.
Step no. 17: Now your function is executing the event, it triggers Lambda function and saves the notification in the DynamoDB table. In the DynamoDB table, you can see the process.
Step no. 18: Go to “Simple Notification Service”, then go to the topic and click on “Publish to Topic”.
Step no. 19: Enter the message and click on “Publish message”. Now your message has been published.
Step no. 20: Go to DynamoDB. Here, you will see the notification of that topic. Click on the notification.
Step no. 21: A pop-up window will appear, and here you will see the information. You can see the SNS Topic ARN, which is the same as the topic ARN that we created.
 This way, we triggered the Lambda Function using SNS notification.

Conclusion

AWS Lambda offers a powerful toolkit for building secure and scalable applications. Lambda is most interesting because one of the most common Amazon services could be threatened by it: EC2, the virtually-based machine service. Instead of spinning EC2 VMs, developers could create apps that run fully on Lambda technology. Using Lambda, Amazon will reinvent itself. AWS Lambda can also be used together with other AWS services, to create a powerful website without the use of a single server or operating system. AWS offers additional serverless capabilities, together with Lambda, so that you can create scalable, efficient, event-driven, secure, safe and economical apps.

Sign-Up with your email address to receive news, new content updates, FREE reports and our most-awaited special discount offers on curated titles !

Loading

Sign-Up with your email address to receive news, new content updates, FREE reports and our most-awaited special discount offers on curated titles !

Loading

Sign-Up with your email address to receive news, new content updates, FREE reports and our most-awaited special discount offers on curated titles !

Loading