Help us understand the problem. What is going on with this article?

terraformでautoscalingしているサーバのcloudwatch alarmを自動設定する

More than 3 years have passed since last update.

aws-scripts-mon

cloudwatchに登録した情報に対してterraformでalarmを設定していきます。

http://aws.amazon.com/code/8720044071969977

aws-scripts-monで--autoscalingをくっつけることでdimensionsにAutoScalingGroupNameをつけさせます。

mon-put-instance-data.pl --mem-util --disk-path=/ --disk-space-util --auto-scaling --from-cron

これをautoscalingで起動しているサーバにcronを登録します。

iamのpolicyにec2:DescribeTags権限がないとAutoscalingGroupNameが取れずにdimensionsに追加されないのに注意が必要です。

autoscaling

下記のようにすればcloudwatchを設定できます。aws_sns_topic.notify_to_slack.arnおよびaws_autoscaling_group.frontは作成済みという前提です。

resource "aws_cloudwatch_metric_alarm" "front_cpu" {
    alarm_name = "${aws_autoscaling_group.front.name} CPU Utilization"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    dimensions { "AutoScalingGroupName"="${aws_autoscaling_group.front.name}" }
    statistic = "Average"
    period = "300"
    threshold = "80"
    alarm_description = "${aws_autoscaling_group.front.name} CPU Utilization"
    alarm_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    insufficient_data_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    ok_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
}

resource "aws_cloudwatch_metric_alarm" "front_mem" {
    alarm_name = "${aws_autoscaling_group.front.name} Memory Utilization"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "MemoryUtilization"
    namespace = "System/Linux"
    dimensions { "AutoScalingGroupName"="${aws_autoscaling_group.front.name}" }
    statistic = "Average"
    period = "300"
    threshold = "90"
    alarm_description = "${aws_autoscaling_group.front.name} Memory Utilization"
    alarm_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    insufficient_data_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    ok_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
}

instance

通常のインスタンスに対してはdimensionsとしてInstanceIdを使います。

aws_instance作成時にcountを使っているならaws_cloudwatch_metric_alarmでもcountを使うことで対応できます。

resource "aws_cloudwatch_metric_alarm" "elasticsearch_cpu" {
    alarm_name = "${element(aws_instance.elasticsearch.*.id, count.index)} CPU Utilization"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "CPUUtilization"
    namespace = "AWS/EC2"
    dimensions { "InstanceId"="${element(aws_instance.elasticsearch.*.id, count.index)}" }
    statistic = "Average"
    period = "300"
    threshold = "80"
    alarm_description = "${element(aws_instance.elasticsearch.*.id, count.index)} CPU Utilization"
    alarm_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    insufficient_data_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    ok_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    count = "${var.elasticsearch_server_count}"
}
resource "aws_cloudwatch_metric_alarm" "elasticsearch_mem" {
    alarm_name = "${element(aws_instance.elasticsearch.*.id, count.index)} Memory Usage"
    comparison_operator = "GreaterThanOrEqualToThreshold"
    evaluation_periods = "2"
    metric_name = "MemoryUtilization"
    namespace = "System/Linux"
    dimensions { "InstanceId"="${element(aws_instance.elasticsearch.*.id, count.index)}" }
    statistic = "Average"
    period = "300"
    threshold = "90"
    alarm_description = "${element(aws_instance.elasticsearch.*.id, count.index)} CPU Utilization"
    alarm_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    insufficient_data_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    ok_actions = [ "${aws_sns_topic.notify_to_slack.arn}" ]
    count = "${var.elasticsearch_server_count}"
}
kanagi
wacul
人工知能でWebサイトの課題を発見する AIアナリスト https://wacul-ai.com を開発しています
https://wacul.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away