how to stop our instance if idle for 30 min in aws cloud watch

1k views Asked by At

I have setup aws cloud watch

Here is below detail set in our cloud watch

  • Metric name: CPUUtilization
  • Statistic: average
  • Period: 5 min
  • Threshold Type: Static
  • Whenever CPUUtilization is Lower/Equal then 10
  • Datapoints to alarm: 1 out of 1
  • Missing data treatment: Treat missing data as missing

EC2 action:

  • Alarm state trigger: In Alarm
  • Take the following action: stop this instance

After 5 min our server still does not stop with the cloud watch in aws.

enter image description here

3

There are 3 answers

0
Raul Barreto On

You can use CloudWatch Events to trigger a Lambda that will perform the stop instance call on the ec2 instance.

0
x89 On

You can create an SQS queue whose target is a Python Lambda function. The python lambda function can use boto to turn off the ec2 instance.

Then you can set this SQS as an alarm action for your Cloudwatch Alarm. You can find more details here:

https://medium.com/geekculture/automatically-turn-off-ec2-instances-upon-inactivity-31fedd363cad

Terraform setup:

https://medium.com/geekculture/terraform-setup-for-automatically-turning-off-ec2-instances-upon-inactivity-d7f414390800

0
Impermanence On

At Feb 2022 you have these options:

  1. You can use Alarms to automate shutdown and terminate instances:

https://aws.amazon.com/blogs/aws-cloud-financial-management/launch-resource-optimization-recommendations/

  1. You can view idle instances in CUR recommendations : https://aws.amazon.com/about-aws/whats-new/2013/01/08/use-amazon-cloudwatch-to-detect-and-shut-down-unused-amazon-ec2-instances/