Linux | Cloud | DevOps | Scripting

Breaking

Friday 23 August 2019

Create a Lambda Function to stop and start an EC2 instance


Like Elastic Beanstalk, Lambda is also an automated version of EC2. But in Lambda we can get access to the operating system but in Elastic Beanstalk, we cannot access to the instance, in this case, we will only get the output and we need to pay only till our code was run.

Lambda runs our code without provisioning or managing servers. The code we run is known as the Lambda function. Lambda Functions can be triggered by multiple events. Lambda uses a very high-level resolution logging mechanism named X-ray. These logs are very detailed and we can see per second changes in the code. AWS Lambda is a 'serverless' compute service.

Serverless means:

1. No servers to provision or manage (means the developers don't need to worry about which AWS resources to launch or how will they manage them).
2. Never pay for idle.
3. Scales with usage (HA/HS).
4. Availability and fault tolerance built-in.

We can bring the Lambda Function (code) from:

1. Python
2. Node JS
3. JAVA
4. C#
5. Go language

Limitation of Lambda:

1. Select power rating from 128MB to 3GB.
2. CPU and Network allocated depends on the size of the code
3. Compute time in Lambda increments by every 100ms.

Create a Lambda Function to stop and start an EC2 instance:

This is a very basic Lambda Function but for better understanding of Lambda Function, we are going to use this. Also, it is very simple code and those who are not familiar with the coding part can also be understand.

For this we need to follow the steps:

1. Create code for Lambda Function to stop the instance
2. Launch two EC2 instances
3. Give names 'prod' and 'peak' to the instances
4. Tag development instance as 'peakhour'
5. Cerate a role
6. Create a Lambda Function
7. Create a trigger for Lambda Function
8. Verify EC2 instance
9. Create a Lambda function to start the instance

Step 1: Create code for Lambda Function to stop the instance:

You can use your own code or can download any pre-build code from GitHub. In this practice I am using below-mentioned code:

--------------------------------------------------------
import boto3
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):
 filter = [
  {
    'Name': 'tag:Type',
     'Values': ['peakhour']
   },
     ]
 instances =ec2.instances.filter(Filters=filter)
 for instance in instances:
  instances.start()
 return 'Success'
--------------------------------------------------------

Step 2: Launch two EC2 instances:

AWS ➔ Services ➔ EC2 ➔ Launch instances ➔ [*] Free tier only ➔ Select Linux AMI ➔ Configure instances ➔ Number of instances: 2 ➔ Review and launch ➔ Launch ➔ Provide key-pair ➔ Launch instance.

Step 3: Give name 'prod' and 'peak' to the instances:

Select instance ➔ under Name click on pencil icon and give name 'prod'. Similarly, select another instance and provide name 'peak'.

Renaming the instance
Fig: Renaming the instance
Now, we are going to perform stop and start activity on Development server by Lambda Function.

Step 4: Tag development instance as 'peakhour':

Select the development instance ➔ Tags: Add/Edit Tags ➔ Create tag ➔ Key: 'Type' ➔ value: 'peakhour' ➔ Save.

Step 5: Create a role:

AWS ➔ Services ➔ IAM ➔ Roles ➔ Create role ➔ Lambda ➔ Next: Permissions ➔ 'EC2FullAccess' and 'CloudWatchFullAccess' ➔ Next ➔ Name: webshack-role ➔ Role description: This role will allow Lambda to fully control on EC2 and Cloudwatch ➔ Create.

Step 6: Create a Lambda Function:

AWS ➔ Services ➔ Lambda ➔ Create a function ➔ [*] Author from scratch ➔ Function name: start_peakhour ➔ Runtime: Python 3.7 ➔ Choose or create an execution role ➔ as we have already created a role, so choose 'Use an existing role' ➔ in Existing role choose role named 'webshack-role', which we created before ➔ Create function.

As the function is created, this will list all those services, which can be accessed by this function.

Access layers in Lambda Function
Fig: Access layers in Lambda Function

Step 7: Create and test a trigger for Lambda Function:

Navigate to the bottom and paste the code here, which we created in step number 1 ➔ Save ➔ Test ➔ as we click on test, we need to give this a name; Event name: TestingFunction (this could be any name) ➔ Create.

Now, we can see 'Execution result: succeeded'. We can also see that there is a link for logs also. So, we can click on this to verify logs.

Step 8: Verify EC2 instance:

AWS ➔ Services ➔ EC2 ➔ refresh the screen and we can see that instance named 'peakhour' is going to stop.

Step 9: Create a Lambda function to start the instance:

Use the same process to start the instance also. But make a little change in the code. Use 'instances.start()' apart from stop in line number 13 and run the code.

Enjoy!





No comments:

Post a Comment

Pages