AWS lambda


What is AWS Lambda?

AWS Lambda helps you in running code without provisioning or managing servers, codes are executed only when needed and it varies from a few per day to a thousand per second and you only have to pay for the compute time you consume and no charge is applied when the code is not running. You are able to run code without any administration for any type of application or backend service with AWS Lambda. AWS Lambda performs all administration of the compute resource and also runs your code on a high-availability compute infrastructure.
AWS Lambda could be used to run code in response to certain events, such as changes to data in an Amazon DynamoDB table or an Amazon S3 bucket; to run the code in response to HTTP requests assisting Amazon API Gateway; or invoke the code with API calls made with AWS SDKs. With such capabilities Lambda can be used to easily build data processing triggers for AWS services such as Amazon S3 and Amazon DynamoDB, process streaming data stored in Kinesis, or could create your own back end that could be operated at AWS scale, performance, and security.



How it works:

AWS Lambda helps you in running code without provisioning or managing servers, codes are executed only when needed and it varies from a few per day to a thousand per second and you only have to pay for the compute time you consume and no charge is applied when the code is not running.



AWS,AWS Lambda,(2020)https://aws.amazon.com/lambda/


Lambda Execution Model:



What can you build with AWS Lambda?

Data Processing:

Real time File processing:
Amazon S3 can be used to trigger AWS Lambda for data to be processed immediately after an upload. For example, Lambda can be used to transcode videos, thumbnail images, process logs,  index files, validate content, and filter and aggregate data in real-time.

Real time stream processing:

AWS Lambda and Amazon Kinesis can be used to process real-time streaming data for application activity tracking, click stream analysis, transaction order processing, metrics generation, data cleansing, indexing,  log filtering, social media analysis, and IoT device data telemetry and metering.

Extract, Transform, Load:

AWS Lambda can be used to perform data validation, sorting, filtering, or other transformations in a DynamoDB table and load the data that has been transformed into another data store.

Backends:

Serverless backends can be built using AWS Lambda in order to handle web, Internet of Things (IoT), mobile, and 3rd party API requests. Lambda’s consistent performance controls, such as Provisioned Concurrency and multiple memory configurations, to build latency-sensitive applications of any scale.

Web Applications:

Developers are able to build powerful web applications that could automatically scale up and down and run in a highly available configuration across multiple data centers – with zero administrative effort by combining AWS Lambda with other AWS services,
IOT Back Ends:
Serverless backends can be built using AWS Lambda to handle web, Internet of Things (IoT), mobile, and 3rd party API requests.

Moble Back Ends:

Rich and personalized app experiences are made easy to create with AWS Lambda. Back ends can be built with AWS Lambda and Amazon API Gateway to authenticate and process API requests. AWS Amplify could be used to integrate the backend with the iOS, Web, Android, and React Native frontends.

Benefits:

No servers to manage:
You just have to write the code and upload it to Lambda,it automatically runs the code without the need to provision or manage servers.

Increased Scale:
AWS Lambda scales your application automatically through running code in response to each trigger. The code runs in parallel and each trigger is processed individually, and scaled  precisely with the size of the workload.

SubSecond metering:
Charges will be for every 100ms the code execute and the no of times the code is triggered. You are charged only for the compute time consumed.

Consistent:
The Code execution time can be optimized by choosing the right size for the function .provisioned concurrency can also be enabled to keep functions initialized and hyper-ready to respond within double digit milliseconds.
AWS,Serverless compute,(2020)https://aws.amazon.com/lambda/

Throwbacks:

Network control
Developers are prevented from controlling networking. In an EC2 environment, updating resolv.conf file would lead to the IP of the server of the other account been displayed. But in Lambda environment Lambda should be connected to VPC (Virtual Private Cloud), the necessary subnet should be selected, VPC DHCP updated in order to display the required server. Hence it makes the process more complicated and time consuming.

Dependencies
External dependencies are featured by many software projects. If features such as video processing and encryption are used the libraries can be really massive and a package dependencies is required If there is no a system-level access the disadvantage with lambda is that this package would have a amximum limit of 50 MB.

Timeout limits
Lambdas has a 300-second timeout limit, where it is disadvantages in implementing certain time consuming tasks

Create a Lambda function:

The desired and closest region should be selected before proceeding with the creation of a Lambda.
The inputs and triggers need to be identified before creating a Lambda function, a running time environment needs to be chosen, and a decision should be taken as to what permissions and role the service will use.


  • Choose Author from scratch
  • Configure the following settings In the Create function screen
  • Choose Author from scratch
  • Function name: myStopinator
  • Runtime: Python 3.8
  • Click Choose or create an execution role
  • Execution role: Use an existing role
  • Existing role: From the dropdown list, choose myStopinatorRole.


Configure the trigger:

A trigger is a resource available in Lambda or any other service configured to invoke the functions with response to a schedule, events of a lifecycle or external requests. A function is able to have multiple triggers and each of these triggers would act as a client invoking your function independently.
AWS,Invoking AWS lambda functios,(2020)https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html

A cloudwatch rule needs to be set up in order to make a Lambda function be automatically trigger by a  CloudWatch event . 




Choose Create a new rule and configure these settings:
  • Rule name: everyMinute
  • Rule type: Schedule expression
  • Schedule expression: rate(1 minute)




Configure the Lambda function:

  • To stop EC2 instance paste following function code editor.

import boto3
region = '<REPLACE_WITH_REGION>'
instances = ['<REPLACE_WITH_INSTANCE_ID>']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))
  • the value of region that you are using and instances should be changed and saved.
  • Verify that an EC2 instance named instance1 is running in your account, and copy the instance1 instance ID.




Verify that the Lambda function worked:

Return to the Amazon EC2 console browser tab and see if your instance was stopped
The Lambda function has successfully processed our request to stop the required instance.
If  you can see that the Instance State is “Stopping” 




The previous steps showed a Lambda function to stop EC2 instance. Here we will learn as to how EC2 instance can be started with Lambda. For this, you can either edit the same function or write a new function following the steps that were used previously.






When you check the details of the execution you will see that the function has been executed successfully. which  means the Lambda function has triggered a request to start the instance. 

How could you modify the Lambda function that you created in the activity, to make it more real-world?

Automatically start/stop EC2 instances with AWS Lambda, Cloudwatch events to manage EC2

AWS offers infrastructure when demanded so that the customers have the ability to control their resources and pay for only what is used hence customers could close a resource when it is not needed and then restart when it is needed. The AWS Instance Scheduler is a AWS-provided solution where customers are allowed to configure custom start and stop schedules easily for  Amazon EC2 and Amazon RDS instances. The solution can be deployed easily and helps reduce operational costs for development as well as production environments. When compared to running these instances 24 hours a day customers using this solution during regular business hours are able to save up to 70%.

Valaxy technologies,(2018,July,18).How to automatically start/stop EC2instances with AWS|Lambda Cloudwatch events to manage EC2[Video].Youtube. https://www.youtube.com/watch?v=EI9mTrEBOJw

Comments