Search code examples
amazon-web-servicesamazon-ec2amazon-cloudwatchamazon-cloudwatch-metrics

Dynamically adding/removing EC2 instances of an AutoscalingGroup to AWS Dashboard Metric Widget


I am trying to create a dashboard and want a widget to display the CPUUtilization graphs of ALL EC2 instances within my autoscaling. Whenever a new instance gets added by autoscaling rules then the dashboard widget should include the graph of new EC2 instance automatically and when an instance gets deleted it should remove the graph of that instance.

When I select the autoscaling group as the metric it simply displays one line in the graph although there are 4 instances within that autoscaling groups.

Is that possible? If yes, what am I doing wrong?


Solution

  • AWS team release an Lambda function to auto update your dashboard : here is the link

    The script:

    • Loads the specified CloudWatch Dashboard(s)
    • Looks for all graph widgets displaying EC2 instance metrics
    • Calls EC2 DescribeInstances API with configured parameters to discover the current EC2 instances for that graph in that region
    • Updates the widget if needed
    • Saves the CloudWatch Dashboards if any widget definition has changed

    But it have also limitation :

    • it will only update graphs whose first metric is an EC2 instance metric, all other metrics on the graph will be replaced with these metrics
    • metrics can not have custom periods or statistics, the graph defaults will be used