What is AWS Lambda?

AWS Lambda is a compute service that lets us run code without provisioning or managing servers. With AWS Lambda, we can run code for virtually any time of application or back-end service all with zero administration. AWS Lambda manages all the administration it manages: -

How Lambda Works?

First of all, we have to upload our code to Lambda in one or more Lambda Function. AWS Lambda will then execute the code on our behalf. After the code is invoked, Lambda will automatically take care of provisioning and managing the required servers.

Differences between AWS Lambda and AWS EC2

AWS Lambda AWS EC2
AWS Lamdba is a Platform as a service AWS EC2 is an Infrastructure as a service.
It supports only limited languages like Node.js, Python, C#, Java, Ruby, Go and Powershell. No environment restrictions, we can run any or code or language.
Write our code and push the code into AWS Lambda. For the first time in EC2, we have to choose the OS and install the required software and then push our code in EC2.
We cannot log into compute instances, choose customized OS or language platform. We can select variety of O.S, instance types, Network and Security Patches, RAM and CPU, etc.
Only pay for the amount of time that our code takes to run. (in multiple of 100 seconds) Pay per second or hourly billing.

Important Terms used in Lambda

  1. Function : A function is a resource that we can invoke to run our code in AWS Lambda. A function has code that processes events, and a runtime that passes request and responses between lambda and the function code.
  2. Runtime: Lambda Runtime allows functions in different languages to run in the same base execution environment. The runtime sits in between the lambda service and our function code, relaying invocation events, context information and responses between the two.
  3. Event: An event is a JSON formatted document that contains data for a function to processes.
  4. Event Source / Trigger : An AWS Service such as Amazon SNS, or a custom service that triggers our function and executes its logic.
  5. Downstream Resource: An AWS service, such as DynamoDB tables or S3 Buckets, that our Lambda Function calls once it is triggered.
  6. Concurrency: No of request that our function is serving in any given time.

Lambda Triggers

We can use AWS Lambda to run our code in response to the event. Events can be anything such as changes to data in an Amazon S3 bucket or an Amazon DynamoDB table. We can also run our code in response to HTTP request using Amazon API Gateway. With these capabilities, we can use lambda to easily build data processing triggers for AWS services like Amazon S3 and Amazon DynamoDB, process streaming data stored in Kinesis or create our own back-end that operates at AWS Scale, performance and security.

Example of S3

When the user creates an object in an bucket, Amazon S3 detects the object created event. Then Amazon S3 invokes our lambda functions using the permission provided by the execution role. Amazon S3 knows which lambda function to invoke based on the event source mapping that is stored in the bucket notification configurations.

Untitled

AWS Lambda Function Configuration

A Lambda function consists of codes and any associated dependencies. In addition, a lambda function also has configuration information associated with it. Initially, we specify the configuration information when we create a lambda function. Lambda also provides an API for us to update some of the configuration data. The Lambda function configuration information includes the following Key elements : -

Services AWS Lambda Can Access

Different Way to invoke Lambda Function

Three different ways to invoke Lambda Function are: -

Synchronous Invoke are the most straight forward way to invoke our Lambda Function. In this model, our functions execute immediately when we perform the Lambda Invoke API Call. Invocation Flag specifies a value of ‘RequestResponse’. We wait for the function to process the event and return a response. Services that support the Synchronous Invoke are➖

  1. Elastic Load Balancer
  2. Amazon Cognito
  3. Cloudfront
  4. API Gateway
  5. Amazon Lex
  6. Kinesis Data Firehose

We can also invoke one lambda function from another lambda function.

For Asynchronous Invocation, lambda places the event in a Queue. It returns a success response without additional information.Lambda Queues the event for processing and returns a Response immediately. We can configure lambda to send an invocation record to another service like SQS, SNS, Lambda and Eventbridge. Services that support the Asynchronous Invoke are ➖

  1. Amazon S3
  2. Amazon SNS
  3. SES
  4. Cloudformation
  5. Cloudwatch Logs
  6. Cloudwatch Events
  7. AWS CodeCommit
  8. AWS Config

Poll based Invocation is designed to allow us to integrate with AWS Stream and Queue based service with no code or server management. Lambda will poll the following service on our behalf, retrieve records on our behalf, retrieve records and invoke our function. The following services support the Poll Based Invocation service ➖

  1. Amazon Kinesis
  2. Amazon SQS
  3. Amazon DynamoDB Streams

AWS Lambda Demo

Inserting the records in Dynamo DB everytime a file gets uploaded to S3 Bucket using AWS Lambda

Untitled

  1. From the AWS Services, search for the Lambda service and click on the Create Function.
  2. We are provided with three options for creating a Lambda Function. For this demo, we will use the “Author from scratch” option.
  3. Then we need to provide the name for our Lambda Function and also provide the programming language.
  4. Now, we will need to choose or create an existing role. We need to select the IAM role that has access to the DynamoDB and then click on create.
  5. After this our Lambda Function gets created.
  6. If we want to change the name of the Handler then we have to change the name of the python file as well.
  7. To view the sample response for the S3 put we can use the “Test” tab in AWS Lambda.

AWS Lambda to read data from only a csv file inside a specific folder in an s3 bucket using trigger

Problem Statement: In this demo, what we will be doing in this demo is that, we will be creating a bucket in S3 inside of which we will be creating another folder. What we want to do is, whenever we upload a csv file in that sub folder, we want our Lambda function to get triggered. And our Lambda function will then have to print the contents of that folder in the console log. But whenever we upload a file outside of that specific sub-folder or whenever we upload any other file other than the csv format, our Lambda function should not get triggered at all.

Untitled

  1. Create an S3 Bucket named babin-training-bucket-1. Inside of this bucket we need to create a folder called data where we will be uploading our csv files.
  2. Now, moving on to the next step where we will be creating a Lambda Function. We first of need to go to AWS Lambda and then we need to Create a Lambda function. For this case, we will be creating a Lambda Function from scratch. For this, we need to provide a name for our Lambda Function, choose the programming language we will be writing our Lambda Function in and then lastly we need to provide a IAM role. For this task, we will be using an existing role that we had access to beforehand and then create a function.