What is AWS Lambda
AWS Lambda is a compute service that lets you run code without provisioning or managing servers.
With AWS Lambda, you can run code for virtually any kind of applications or backend service -all with Zero administration.
AWS Lambda manages all the administrations including following.
1.Provisioning and capacity of the complete fleet that offers a balance of memory, CPU, network and other resources.
2.Server and OS maintenance.
3.High Availability and Automatic Scaling.
4.Monitoring fleet health
5.Applying security patches
6.Deplyoing your code
7.Monitoring and logging Lambda functions
8.AWS Lambda runs your code on a high availability compute Infrastructure.
AWS Lambda runs your code on a high availability compute Infrastructure
AWS Lambda executes your code only when needed and scales automatically from a few requests per day to thousands per second.
You pay only for the compute time you consume-No charge when your code is not running.
All you need to do is supply your code in the form of one or more Lambda function functions to AWS Lambda, in one of the languages that AWS Lambda supports (Node Js, Java, Powersheel,,C#, Ruby , Python and Go) and the service can run the code on your behalf.
IAM Role—This is the role that AWS Lambda assume when it executes the Lambda function on your behalf.
Typically, the lifecycle for an AWS Lambda based applications includes authorizing code, deploying code to AWS Lambda and then monitoring and troubleshooting.
This is in exchange for flexibility which means you can not log into compute instances or customize the operating system or language runtime.
If you want to manage your own compute, you can use EC2 or Elastic Beanstalk.
How Lambda Works
- First you upload your code to Lambda in one or more Lambda functions.
- AWS Lambda will then execute the code in your behalf
- After the code is invoked, Lambda automatically take care of provisioning and Managing the request servers.
AWS Lambda Vs AWS EC2
|AWS Lambda||AWS EC2|
|AWS Lambda is platform as a service||AWS EC2 is infrastructure as a service|
|It supports only limited languages like Node Js, Python, Java, C#, Ruby, Go and PowerShell||No Environment restrictions, runs any code or languages|
|Write your code and push the code to AWS Lambda||For the first time in EC2, you have to choose the OS and install all the software required and then push the code in EC2|
|You can not login to Compute instances, choose customized OS or language platform||You can select Variety of OS, instance types, network and security patches, RAM and CPU etc.|
Important terms used in Lambda:
Functions: A function is a resource that you can run your code in AWS Lambda .A function has code that process events and runtime that passes requests and responses between Lambda and 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 your function code. Relaying invocation events, content information and responses between the two.
Event: Event is a JSON formatted document that contains the data for a function to process.
Event Source/Trigger: An AWS service such as Amazon SNS or a custom service that triggers your function and executes its logic.
Downstream Resources: An AWS Service such as DynamoDB tables or S3 buckets that your Lambda function calls once it is triggered.
Concurrency: Number of requests that your function is serving at any given time.
When Lambda Triggers:
You can use AWS Lambda to run your code in response to :
Events such as changes to data in an Amazon S3 bucket or an Amazon DynamoDB table.
To run your code in response to HTTP request using Amazon API gateway.
With these capabilities you 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 your own backend that operates at AWS Scale, performance and Security.
- The user creates an object in a bucket
- Amazon S3 invokes your Lambda functions using the permission provided by the execution role.
- Amazon S3 knows which Lambda function to invite based on the event source mapping that is stored in bucket notification configuration.
AWS Lambda function configuration:
A Lambda function consists of code and any associated dependencies.
In addition, a Lambda function has configuration information associated with it.
Initially you specify the configuration when you create a Lambda function.
Lambda provides an API for you to update some of the configuration data.
Lambda Function Configuration information includes the following key elements:
1.Compute resources that you need.
You only specify the amount of memory you want to allocate from your Lambda function.
2.AWS Lambda allocates CPU Power proportional to memory by using the same ratio as a general-purpose amazon EC2 instance type, Such as an M3 Type.
3.You can update the configuration and request additional memory in 64 MB increments from 128MB to 300 MB.Functions larger than 1536 MB are allocated Multiple CPU threads.
AWS Lambda function—Services it can access:
Lambda function can access
- AWS Services or Non-AWS services
- AWS Services running in AWS VPC (RedShift, ElasticCache, RDS instance)
- Non-AWS services running on EC2 instances in an AWS VPC.
AWS Lambda run your function code securely within a VPC by default.
However, to enable your Lambda function to access resources inside your private VPC, you must provide additional VPC-Specific configuration information that includes VPC subnet ID and Security group IDs.
Different Ways to Invoke Lambda function:
1.Synchronous Invoke (Push)
- Synchronous invoke are the most straight forward way to invoke your Lambda function.
- In this model, your function executes immediately when you perform the Lambda Invoke API call.
- Invocation flag specify a value of “Request Response”
- You wait for the function to process the event and return a response.
Here is the list of services that invoke Lambda function Synchronously.
- Elastic Load balancer
- Amazon Cognito
- API gateway
- Amazon Lex
- Kinesis firehose.
2.Asynchronous Invocation: Event Based.
Asynchronous invokes places your invoke request in Lambda Service queue and process the request as they arrive. You can use AWS Xray to review how long your request spent in the service queue by checking the :dwell time segment”.
For asynchronous Invocation, Lambda places the request in a Queue and returns a success response without additional information. Does not wait for function2 to finish or respond.
Lambda queues the event for processing and returns a response immediately.
You can configure Lambda to send invocation record to another service like SQS, SNS, Lambda and Event bridge.
Here is the list of Services that invoke Lambda function asynchronously:
- Amazon S3
- Amazon SNS
- Cloud formation
- CloudWatch logs
- CloudWatch events
- AWS Code commit
- AWS config
3. Poll Based Invokes: Poll Based
The invocation model is designed to allow you to integrate with AWS stream and Queue based service with no code or server management. Lambda will poll following services on your behalf, retrieve records and invoke your functions.
Following are supported Services:
- Amazon Kinesis
- Amazon SQS
- Amazon DynamoDB streams.
You pay for the AWS resources that are used to run your Lambda function.
To prevent your Lambda function from running indefinitely, you specify timeout.
When the specified timeout is reached, Lambda terminates your Lambda function.
Default is 3 seconds and maximum is 900 seconds (15 min)
Duration is calculated from the time your code begins executing until it returns or otherwise terminates, rounded upto nearest 1ms. The price depends on the amount of memory you allocate to your functions and are allocated proportional CPU power and other resources.
1.AWS Pricing based on Requests and Duration:
|Requests-Number of Requests for your function||$0.20 per 1M requests.|
|Duration-The time it takes for your code to execute.||$0.0000166667 for every GB Seconds.|
Compute Saving plan—AWS offers compute saving plan a flexible pricing model that offer low prices on EC2, Fargate and Lambda usage, in exchange for a commitment to a consistent usage ($/hour) for a 1 or 3 year term.—Can save up to 17% on AWS Lambda.
The AWS Lambda free usage tier includes 1M free request per month and 400000 GB seconds of compute time per month.
2.Data Transfer and Other charges: Data transferred in and out of your AWS Lambda function from outside the region will be charged at EC2 data transfer rates. Charges for reading and writing data and for data stored in Amazon S3.
- Functions using provisioned concurrency are pre-provisioned in advance and ready to serve request as they come in.
- No cold start timing-There is no unneeded initialization in following invocations. Initialized environments are hyper ready to react in double digit milliseconds.
- Time spent on execution is less.
- Saving in pricing as execution time is reduced.