Search code examples
amazon-web-servicesamazon-ec2aws-cliamazon-cloudwatch

AWS CLI command to get list of ec2 instances filtered according to no. of alarms attached


I have to delete the some redundant alarms attached to the EC2 instances. For that I need to filter those EC2 instances which have more than 2 alarms attached. But I am not finding any filter provided by AWS through which I can filter the instances based on the attached alarms. Also, describing alarms command would not help here as to every instance a unique alarm is attached.


Solution

  • This will give a list with all ec2 instances defined in CW alarms dimensions in your account (equivalent to inaccurate term of attached):

    aws cloudwatch describe-alarms --query 'MetricAlarms[*].Dimensions[?(Name==`InstanceId`)].Value' --output text | sort
    

    The sort will give you the list ordered, so any repeated instanceId, that means it has more than 1 alarm.