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
Duration
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))
No comments:
Post a Comment