最近ブログネタがありません。(ウソです)
今回はCloudWatchをterraform化してみました。
何台もインスタンスある場合手動で設定するのはくそめんどくさいので
こういうときにterraformは重宝するかと。
■aws_cloudwatch_metric_alarm
https://www.terraform.io/docs/providers/aws/r/cloudwatch_metric_alarm.html
今回やりたいこととしては
・ステータスチェック
・CPUが80%以上であればアラート
■aws_variables.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
## CloudWatch variable "metric_name-CPU" { default = "CPUUtilization" } variable "metric_name-Status" { default = "StatusCheckFailed" } variable "alarm_actions" { default = "arn:aws:sns:ap-northeast-1:xxxxxxxxx:awsweb" } variable "namespace" { default = "AWS/EC2" } variable "comparison_operator" { default = "GreaterThanOrEqualToThreshold" } variable "evaluation_periods" { default = "2" } variable "period" { default = "300" } variable "statistic_Ave" { default = "Average" } variable "statistic_Max" { default = "Maximum" } variable "threshold_CPU" { default = "80" } variable "threshold_Status" { default = "1" } |
ここらへんはメンテしやすいように変数化しました。
■aws_cloudwatch.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
## Cloudwatch(adachin-web01) resource "aws_cloudwatch_metric_alarm" "adachin-web01_CPU" { alarm_name = "${aws_instance.adachin-web01.tags.Name}_CPUUtilization" comparison_operator = "${var.comparison_operator}" evaluation_periods = "${var.evaluation_periods}" metric_name = "${var.metric_name-CPU}" namespace = "${var.namespace}" period = "${var.period}" statistic = "${var.statistic_Ave}" threshold = "${var.threshold_CPU}" alarm_description = "${aws_instance.adachin-web01.tags.Name}(${aws_instance.adachin-web01.id}) cpu utilization" insufficient_data_actions = [] alarm_actions = ["${var.alarm_actions}"] treat_missing_data = "missing" dimensions { InstanceId = "${aws_instance.adachin-web01.id}" } } resource "aws_cloudwatch_metric_alarm" "adachin-web01_Status" { alarm_name = "${aws_instance.adachin-web01.tags.Name}_high-status-check" comparison_operator = "${var.comparison_operator}" evaluation_periods = "${var.evaluation_periods}" metric_name = "${var.metric_name-Status}" namespace = "${var.namespace}" period = "${var.period}" statistic = "${var.statistic_Max}" threshold = "${var.threshold_Status}" alarm_description = "${aws_instance.adachin-web01.tags.Name}(${aws_instance.adachin-web01.id}) high-status-check" insufficient_data_actions = [] alarm_actions = ["${var.alarm_actions}"] treat_missing_data = "missing" dimensions { InstanceId = "${aws_instance.adachin-web01.id}" } } |
■terraform apply
ちゃんと取れてるのでOK!
■まとめ
もともとterraform importして今回変数化したので、
圧倒的に工数が下がった!
0件のコメント