- Priya
- October 10, 2022
Step By Step guide to auto stop ec2 instance using lambda function on amazon web service
Hello,I hope you’re doing well.
In this blog we are discussing how to auto stop running ec2 instance using lambda function on aws console.Lambda is is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services.Using lambda function, we can easily schedule EC2 instances’ start and stop time to save AWS cloud operational cost.We need to setup a trigger on lambda function to schedule auto stop ec2 instances.
There are some steps to auto stop ec2 instances using lambda function:
Launch a EC2 instance
- Login to AWS console management.
- Go to AWS Services & select Compute.
- Click on EC2.
- Click on the following below URL for launch a EC2 instance on aws console.
https://www.hackerxone.com/2021/05/21/how-create-ec2-instance-amazon-web-serviceaws/
- Once Instance is launched successfully.
Create a Lambda function
- Go to Services & select Compute.
- Click on Lambda.
- Click on the following below URL for create a Lambda function.
https://www.hackerxone.com/2021/05/30/how-create-lambda-function-amazon-web-serviceaws/
- Lambda function is created.
Attach a IAM policy on Lambda function Role
- To attach the following IAM policy on Lambda function role.
“AmazonEC2FullAccess”
- Here is the output.
Test Python code on lambda function for Auto Stop EC2 instance
- Go to Created lambda function.
- Under the test folder you will find a lambda_function.py file.Click in that file.
- Remove the existing code.
- Add the New code.
import json
import boto3
ec2 = boto3.resource('ec2', region_name='eu-west-3')
def lambda_handler(event, context):
instances = ec2.instances.filter(Filters=[
{
'Name': 'instance-state-name',
'Values': ['running']
},
])
for instance in instances:
id=instance.id
ec2.instances.filter(InstanceIds=[id]).stop()
print("Instance ID is started :- "+instance.id)
return "success"
- Click on Deploy.
- Code is deployed.
Create a Event
A Event is a JSON object that mocks the structure of request emitted by AWS service to invoke a lambda function.
- Next ,Click on Test.
- Select event action “create a new event”.
- Provide a Event Name.
- Click on Save.
- The Event was successfully saved.
Configure the Lambda function
- Go to Configuration.
- In the General configuration, click Edit.
- By default the timeout is 3 sec.
- Increase the timeout and click on Save.
- Lambda function has been updated.
Test the code
- Click on code option & click on Test.
- Now, code is executed successfully.
Check the Instance status
- Go to EC2 instance.
- Here,the status of Instance is stopping.
Add a trigger on lambda function
- Click on Add Trigger.
- Select a source “EventBridge (CloudWatch Events).
- Click on “create a new rule”.
- Provide a rule name & description.
- Select Rule type “Schedule Expression”
- In Schedule repression,mention — cron(0 22 ? * * *) – Everyday at 10:00 PM UTC.
- In the mentioned Region on code,all EC2 instances will stop automatically daily at 10:00 p.m. UTC.
- Click on Add.
- The Trigger was successfully added to lambda function.