Serverless compute on AWS
AWS Lambda is a compute service from Amazon Web Services that allows you to run code without providing or managing servers. With AWS Lambda, you can run code for almost any type of application or backend service with no need for an administrator.
Aws manages various administrator services:
- Provisioning and capacity of the compute fleet that offers a balance of memory, CPU, network, and other resources.
- Server and OS maintenance
- High availability and Automatic Scaling
- Monitoring fleet Health
- Applying Security patches
- Deploying your code
- Monitoring and logging your lambda function
- AWS Lambda runs your code on a high–availability compute infrastructure
AWS Lambda runs our code on a high–availability compute infrastructure. It executes code only when needed and scales up automatically as per the load on the application. We pay only for the compute time so no extra charges. Also prerequisite is we have to upload our code in form of a lambda function to AWS lambda. Lambda supports various languages such as c#, Ruby, Python, Go, python, java, and node js.
The lifecycle for an AWS lambda-based application includes authoring code, deploying code to AWS lambda, and monitoring and troubleshooting. In this, we can’t log into instances or customize the O.S or language runtime. If we want to manage our own instance we can use EC2 instance or Elastic beanstalk instead of lambda.
## How lambda works???
First, upload code into one or more Lambda functions.
Second, lambda will execute code by itself for us.
Third, When code is invoked, lambda take care of provisioning and managing all type of services.
## Important terms in lambda =>
1. Function: A function is a resource that you can run to invoke your code in AWS lambda. Function consisting code that processes the events, and runtime env that passes request and responses between lambda and function code.
2. RUNTIME: It allows functions to run in different languages with the same base environment. The runtime works as an intermediate between lambda service and function code, relaying invocation events, contact information, and responses between the two.
3. EVENT: It’s a JSON formatted document that contains data for a function to process.
4. Event Sources/Trigger: Services such as Amazon SNS, or a custom service that triggers your function and executes the logic.
5. Downstream resource: An AWS service such as dynamo DB table or S3 buckets, your lambda function calls once triggered.
6. Concurrency: No. request served by the function in a given time.
## When Lambda triggers?
We can use AWS Lambda to run your code in response to:-
- Events such as a change in data in Amazon S3 bucket or an Amazon DynamoDB table.
- To run your code in response to HTTP requests using Amazon API gateway.
- With these capabilities, we can use lambda to easily build data processing triggers
## AWS lambda function configuration
A lambda function consists of code and any associated dependencies.
In addition, a lambda function also has configuration information associated with it.
Initially, we can define the configuration information when we create a lambda function.
Lambda function provides an API for us to update some of the configuration data.
## Lambda function configuration information includes the following key elements:
Compute resources that you need- we can only specify the amount of memory [RAM] we want to allocate for the lambda function.
AWS lambda allocates, CPU, power proportional to the memory by using the same ratio as a general-purpose Amazon EC2 instance type, such as M# type.
We can update the configuration and request additional memory in 64 MB increments from 128 MB to 3008 MB.
Functions larger than 1536Mb are allocated multiple threads.
## Maximum Execution time
You pay for the AWS resources that are used to run your lambda function. To prevent your lambda function from running indefinitely we specify a timeout. When the specified timeout is reached, AWS lambda terminates your lambda function. Default is 3 seconds and the maximum is 900 sec [15 mins].
IAM Role => This is the role that AWS lambda assumes when it executes the lambda function on our behalf.
## Services accessed by AWS lambda Function:
- AWS services or Non-AWS services
- AWS services running on EC2 instances in an AWS VPC.
- AWS Lambda runs our code securely within a VPC by default.
- However, to enable your Lambda function to access resources inside your VPC, you must provide additional VPC-specific configuration information that includes VPC subnet ID and security group Ids.
## Different ways to invoke lambda function.
- Synchronous invoke(push)
They are the most straightforward way to invoke your Lambda function. In this model, your function executes immediately when you perform the invoke API call.
The invocation flag specifies a value of “RequestResponse”. We wait for the function to process the event
And return a response.
2. Asynchronous invoke(Event)
Here, Lambda places the event in a queue and returns a successful 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 event bridge.
3. Poll based invokes (Pull based):
The invocation model is designed to allow you to integrate with AWS stream and queue-based services with no code or server management, lambda will poll the following services on our behalf, retrieve the record and invoke your function. Supported services are Amazon Kinesis, SQS, DynamoDB Streams.