Use AWS CloudWatch to start stop EC2 instances How to implement it

+1 vote

I am trying to figure out ways to start/stop a AWS EC2 instance in case of CPU utilization increase or decrease on another EC2 instance.

I am familiar with service available Auto Scaling in AWS but I dont want to use these services.

So just looking if it is possible.

Just detailing the concern like suppose I have 2 EC2 instance on AWS account by name EC21 and EC22. By default, EC22 instance is stopped.

Now I need to setup CloudWatch to check if load/CPU utilization increase on EC21 instance by 70% then need to start EC22 server and similarly if load decrease on EC21 instance by 30% then stop EC22 server.

Please help!

Oct 26, 2018 in AWS by findingbugs
• 3,260 points
4,230 views

2 answers to this question.

+1 vote

What you can do is set a CloudWatch alarm and check if it is triggered, it will notify an SNS topic. 

You can have that SNS topic then invoke a Lambda function, which can then start your EC2 instance.

All you need to do is follow the following steps and you are good to go.

  1. Create an AWS Lambda function that starts your EC2 instance.
  2. Configure your SNS topic to invoke your Lambda function when it receives messages. You can read about that here: Invoking Lambda functions using Amazon SNS notifications
  3. Finally, ensure your CloudWatch alert sends messages to the SNS topic.
Hope this helps.
answered Oct 26, 2018 by Priyaj
• 58,020 points
Hi Priyaj, Your solution of Start/Stop EC2 instance depending on the CPU utilization of another EC2 instance looks good. Can you please share the code of Lambda and if possible step-by-step guide how configure SNS and cloudwatch etc.

Many Thanks in advance.

Hi Hasan, this lambda function would do the job:

import boto3
region = 'es-east-1'
instances = ['id']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))

Have a look at this to configure CloudWatch: https://www.edureka.co/blog/amazon-cloudwatch-monitoring-tool/

And, have a look at this to create an SNS topic: https://www.edureka.co/community/46817/create-topic-creating-alarm-balancer-using-cloudwatch-console

0 votes
###EC2 Stop

import boto3
# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'ap-south-1'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['i-0ca0d2700eeeckjdk']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.stop_instances(InstanceIds=instances)
    print 'stopped your instances: ' + str(instances)

### EC2 Start

import boto3
# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'ap-south-1'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['i-0dc0911b02aaae3b']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.stop_instances(InstanceIds=instances)
    print 'stopped your instances: ' + str(instances)
answered Sep 11, 2020 by Shrikant
• 180 points

Related Questions In AWS

+1 vote
2 answers
0 votes
1 answer

How to stop EC2 instances using Boto3?

Hi@akhtar, To change the state of an EC2 ...READ MORE

answered Oct 7, 2020 in AWS by MD
• 95,460 points
2,275 views
0 votes
1 answer

How to find exact stopped time of AWS EC2 instances?

Hi@Lakshminarayanan, You can use InstanceState Data Types in your ...READ MORE

answered Dec 3, 2020 in AWS by MD
• 95,460 points
3,336 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

AWS EC2 Free Tier- how many instances can i run?

Amazon has a handy page that details ...READ MORE

answered Jul 6, 2018 in AWS by Priyaj
• 58,020 points
3,062 views
0 votes
1 answer

How to get AWS account/service cost using CloudWatch API?

You can check this link for a ...READ MORE

answered Jul 13, 2018 in AWS by Priyaj
• 58,020 points
923 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP