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,200 points

1 answer 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
• 57,550 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):
    print('stopped your instances: ' + str(instances))

Have a look at this to configure CloudWatch:

And, have a look at this to create an SNS topic:

Related Questions In AWS

+1 vote
2 answers
+2 votes
2 answers

Is it possible to ping AWS EC2 instance

Start by adding a new EC2 security group inbound ...READ MORE

answered Apr 9, 2018 in AWS by hemant
• 5,760 points
0 votes
1 answer

Is it possible to stop nodes in AWS ElastiCache cluster

At this time there is not a ...READ MORE

answered Jun 20, 2018 in AWS by Flying geek
• 3,210 points
0 votes
1 answer

How to link AWS Lambda function to Amazon CloudWatch ?

In order to create Log Group and ...READ MORE

answered Jul 20, 2018 in AWS by datageek
• 2,460 points
0 votes
1 answer
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
• 57,550 points
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
• 57,550 points