1

I am trying to set up monitoring of a large number of ec2s and their number is constantly changing. I would like the owner of this instance to receive a notification when the CPU usage is low for a long time.

I can create a function that would get a list of all ec2s, then get their CPU utilization, then send messages to the owners. This option does not suit me, since it takes some time to monitor the state, and not get the CPU utilization values per second of the function launch. And in general, this method looks bad.

I can set up alarm in cloudwatch, but only for one specific instance. This option is not suitable, since there are a lot of ec2 and their number varies.

I can create a dashboard with ec2 names and their CPU utilization. This dashboard will be dynamically replenished. But I haven't figured out how to send notifications from it.

How can I solve my problem without third-party solutions?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Shootnik
  • 31
  • 1
  • 5
  • Why the number changes ? Are the ec2s in autoscaling group? If not, you can create a lambda which checks new ec2 instances and set up an alarm whenever it finds one. – Riz Jan 17 '22 at 15:10
  • @Riz New instances are created or deleted for any reason. Someone can create them themselves, or maybe it's autoscaling. I guess I didn't put it quite right. The main task is to identify underutilized instances and send messages to instance owners. – Shootnik Jan 17 '22 at 15:16
  • The only option I can think of is a lambda which run every hour/day/week(depends on your requirement) and checks new instances and create an alarm based on the name of the EC2 or any unique tag. This lambda(or another one) needs to take care of unused alarms as well. It needs to delete the alarms 'whose' EC2 doesn't exist anymore. – Riz Jan 17 '22 at 15:41

2 Answers2

1

Please see this AWS document https://aws.amazon.com/blogs/mt/use-tags-to-create-and-maintain-amazon-cloudwatch-alarms-for-amazon-ec2-instances-part-1/

You will find some existing Lambda functions which will create Cloudwatch alert after creating EC2 instance automatically.

It looks a little bit tricky but worth seeing if you really want to make it automatic. But yes single cloud watch alert can't monitor multiple EC2 instances.

--

Another thing, same sample lambda function you will find from the existing template and it will directly create that lambda function and you can test it.

helper
  • 176
  • 1
  • 1
  • 7
1

I have solved my problem. And it seems to me that this is one of the simplest options.

Using method get_metric_data from AWS SDK for Python Boto3 I wrote a function:

import boto3
from statistics import mean
from datetime import timedelta, datetime

cloudwatch_client = boto3.client('cloudwatch')

response = cloudwatch_client.get_metric_data(
    MetricDataQueries=[
        {
            'Id': 'myrequest',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/EC2',
                    'MetricName': 'CPUUtilization',
                    'Dimensions': [
                        {
                            'Name': 'InstanceId',
                            'Value': 'i-123abc456def'
                        }
                    ]
                },
                'Period': 3600,
                'Stat': 'Average',
                'Unit': 'Percent'
            }
        },
    ],
    StartTime=datetime.now() - timedelta(days=1),
    EndTime=datetime.now()
)
for MetricDataResults in response['MetricDataResults']:
    list_avg = mean(MetricDataResults['Values'])
    print(list_avg)

At the output, I get the average CPU usage as a percentage. For the specified time.

I'm still learning, but I'll try to answer your questions if there are any. Thank you all!

Shootnik
  • 31
  • 1
  • 5