Visitor Counter

Visitor Counter

Saturday, May 2, 2020

Introduction to AWS Lambda









What is LAMBDA?

Lambda came to the AWS in 2015.  It was released in the Re-invent 2015. It was a game-changer because people do not want to worry about managing Data centers, IAAS, PAAS, and managing Containers cause amazon take care all of that. The only thing you have to worry about is your Code, Upload your code to Lambda and you set an event trigger. Actually, it's like Encapsulating your Data Center, Hardware, Operating System and Assembly Code is Encapsulate by AWS Lambda, We don't have to worry about manging anything


What Languages Lambda support

 

  • Node.js

  • Java

  • Python

  • C#

  • Ruby

  •  

How Lambda Priced?

  • Number Of Requests

 First 1 Million Request are free and thereafter $0.20 per 1 million Request

  • Duration

 Duration is Calculate from the time that code beginnings to executing and until it's terminated. It will depend on how much memory (GB) allocated for your function.


Why Lambda is worth

  • No Severs

  • Continuous Scaling

  • Cheep




Scenario -:Create a Lambda function Using Cloud watch to stop the Instances within 1 Minute

 

 

To do this you have to log in to your Amazone account







Creating a Lambda Function




To create Lambda  Function Select Lambda Under the Compute or We can use the search bar to get the Function and select it







 Select Create Function




You will get this Window when you Select the Create Function in this window, The Default selection would be Author from scratch  we are going to use that Now we have to add some details as Below


Function name: myStopinator

Runtime: Python 3.8 (The Coding language that we are going to use to create the function )


Under the Permission Select


Execution role: Use an existing role

Existing role:myStopinatorRole





Adding the Trigger




Now We are going to add a trigger for the function. It's like a Schedule Task in Windows OS


To create the trigger

We have to select a trigger from the list and the details below

 from drop-down  list

 

    Select Cloud Watch ( Cloud watch is a function that Moniort EC2 Instances Set alarms, Set and store Logs, Monitor and react to resource changes ) Using Cloud Watch are going to stop an instances Cloud watch help to resource utilization, application performance and much more

    Rule Create a New Rule


    Rule name: everyMinute


    Rule type: Schedule expression


    Schedule expression: rate(1 minute)  
(from here we can manage the time that function will trigger )


If we like We can give a description for the Rule

after adding all the details Click ADD




 Configure the Lambda function






To Configure the function we have to add these codes. To get this Window in the design box select the LAMBDA function that you have created myStopinator and select edit then you will get this function box and paste this code 


NOTE-: 

Replace the below details


Region = Region that you are currently in (Keep the single quotation marks (' ') around the Region)



Instances = Get the Instances ID (Keep the single quotation marks (' ') around the Instances) check how to get the incase ID if you don't know the ID of your Instances



After adding the above details click Save button in the Top right Coner 



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))





 How to get the Instance ID




Go to the services and select EC2 and Select Instances in the Instances window Select the Description tab and you will be able to get your Inctanses ID







 After saving click the Monitor In the Left Conner






To get a verify go to instance tab and check instances should be STOPED







 If you try to start it again it will stop within 1 minute














This because we mention in the function Schedule to run the function every 1-minute

expression: rate(1 minute)

in the Code, we have mentioned to stoped the Instances 

"ec2.stop_instances(InstanceIds=instances) "




More about Lambda

We can automate our work using lambda it's serverless (S3, API gateway, Dynomore DB, Cloud Watch, Cloud Front, etc.. A few of the services that support Lambda  ) We just have to upload the code and select the proper function to trigger the function. We do not need any Sever administrators, Database administrates at all, it Scales Out automatically, like adding more and more Instances for load balancing If we are getting more and more load it will Scales Out rather than Scaling Up. 

 

Scales Out, For instance, If we have a website that only has one Lambda function and it's getting a million users hit at once, requesting that function, automatically million different functions will deploy and that will be returned to the user. 

 

Thinking a Real word Example we can use-:

We can easily create a  basic Serverless web site using lambda function we do not want to worry about any severs web administrators at all. We just have to upload the code using the Languages that Lambda support ( Java, Python, etc..) and upload your '.html 'files to an S3 bucket. If we have your own domain we can connect that domain using Route53 or we can use the default link to access the web.

 

In this example, we have used the Cloud Watch Function to trigger a Python Code to stop Instances within 1 minute.

 

So How it's going to help us?

In the AWS for every service we have to pay (Uptime) and the best way to save our money is if we are not using any of the services turn it off. in this scenario, if we are not using the instances we can create a Lambda function to stop the services and we can set a time like if we are not using this in the weekend we can trigger a function to stop it in the weekends.










References -:


https://www.amazonaws.cn/en/cloudwatch/


https://docs.aws.amazon.com/lambda/latest/dg/welcome.html









No comments:

Post a Comment

Click hear to Visit my Facebook