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: -
Triggers) and scales automatically, from a few requests per day to thousands per second. Also, we pay only for the compute time we consume. So we are not charged for time when our code is not running.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.
| 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. |
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.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.Event: An event is a JSON formatted document that contains data for a function to processes.Event Source / Trigger : An AWS Service such as Amazon SNS, or a custom service that triggers our function and executes its logic.Downstream Resource: An AWS service, such as DynamoDB tables or S3 Buckets, that our Lambda Function calls once it is triggered.Concurrency: No of request that our function is serving in any given time.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.
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.

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 : -
IAM role, and this is the role that AWS Lambda assumes when it executes the lamdba function on our behalf.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➖
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 ➖
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 ➖

Lambda service and click on the Create Function.Author from scratch” option.Problem Statement: In this demo, what we will be doing in this demo is that, we will be creating a bucket in
S3inside of which we will be creating another folder. What we want to do is, whenever we upload acsvfile 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 thecsvformat, our Lambda function should not get triggered at all.

babin-training-bucket-1. Inside of this bucket we need to create a folder called data where we will be uploading our csv files.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.