AWS にアカウントを作った時は Billing Alert を設定したいですが、下記のような3段階で設定しているので結構めんどくさいです。
- 月はじめに必ず通知される程度の金額(1 USD)
- 予想する請求額
- ↑の3倍ぐらいの金額
ので、Terraform で一撃で Billing Alert を設定するようにしました。
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
variable "billing_name" {
default = "ore no billing"
}
variable "billing_threshold" {
default = [
1,
10,
100,
]
}
resource "aws_sns_topic" "billing" {
provider = "aws.us-east-1"
name = "BillingAlarm"
}
resource "aws_cloudwatch_metric_alarm" "billing" {
provider = "aws.us-east-1"
alarm_name = "${var.billing_name} lv.${count.index + 1} (${var.billing_threshold[count.index]} USD)"
namespace = "AWS/Billing"
metric_name = "EstimatedCharges"
statistic = "Maximum"
evaluation_periods = "1"
comparison_operator = "GreaterThanOrEqualToThreshold"
period = "21600"
threshold = "${var.billing_threshold[count.index]}"
dimensions { "Currency" = "USD" }
alarm_description= "Total Charge ${var.billing_threshold[count.index]} USD"
alarm_actions = [ "${aws_sns_topic.billing.arn}" ]
count = "${length(var.billing_threshold)}"
}
-
provider
でregion
にus-east-1
を指定する- Billing Alert は
us-east-1
リージョンじゃないとだめ - 他のリソースは
ap-northeast-1
なのでus-east-1
でエイリアス
- Billing Alert は
-
billing_name
でアラームの名前を指定する- メールの件名に含まれたりするのでわかりやすい名前をつける
-
billing_threshold
は通知する金額のしきい値(USD)- リストで複数指定する
アラームは BillingAlarm
という SNS トピックに通知されます。
このトピックからメールで通知しているようにしているのですが、トピックへのメールアドレスのサブスクライブは手作業で行っています。Terraform では SNS トピックへのメールアドレスのサブスクライブはできないためです。
Lambda 経由で Slack とかにした方が良いかも。