How to launch EC2 instance with preconfigured AWS Alarm

amazon-cloudwatchamazon-web-servicescloudscalability

I wan't to kill a specific instance when some of it's hardware consumption metrics reaches a certain level. If i create an alarm for the scaling group (setting maximum cpu consumption threshhold to >=50 for example) it will kill the oldest instance – not the one that is misbehaving. One way to kill a specific instance is to create an alarm for the instance instead of creating it for the scaling group. However, if i launch a new instance (with autoscaling), this new instance will not have the alarm.

Is there a way to launch instances with preconfigured ec2 level alarms?

Best Answer

Cloudformation's AWS::CloudWatch::Alarm is useful for setting up machine-specific alarms. It's especially great combined with autoscaling, ELB, and EC2 instances. Here's a snippet used with an autoscaling group. Perhaps someone can suggest an edit to give an EC2-specific example.

"appCPUAlarmHigh": {
  "Type": "AWS::CloudWatch::Alarm",
  "Properties": {
    "EvaluationPeriods": "7",
    "Dimensions": [
      {
        "Name": "AutoScalingGroupName",
        "Value": {
          "Ref": "appServerGroup"
        }
      }
    ],
    "AlarmActions": [
      {
        "Ref": "appStatusTopic"
      }
    ],
    "AlarmDescription": "Notify if CPU high for >  7m",
    "Namespace": "AWS/EC2",
    "Period": "60",
    "ComparisonOperator": "GreaterThanThreshold",
    "Statistic": "Average",
    "Threshold": "50",
    "MetricName": "CPUUtilization"
  }
},

Otherwise, you could roll something with Cloudinit and mon-enable-alarm-actions. Or OpsWorks using, uh, I don't know.

Related Topic