9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Terraform で AWS の Billing Alert(請求アラート) を設定する

Last updated at Posted at 2016-01-20

AWS にアカウントを作った時は Billing Alert を設定したいですが、下記のような3段階で設定しているので結構めんどくさいです。

  1. 月はじめに必ず通知される程度の金額(1 USD)
  2. 予想する請求額
  3. ↑の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)}"
}
  • providerregionus-east-1 を指定する
    • Billing Alert は us-east-1 リージョンじゃないとだめ
    • 他のリソースは ap-northeast-1 なので us-east-1 でエイリアス
  • billing_name でアラームの名前を指定する
    • メールの件名に含まれたりするのでわかりやすい名前をつける
  • billing_threshold は通知する金額のしきい値(USD)
    • リストで複数指定する

アラームは BillingAlarm という SNS トピックに通知されます。

このトピックからメールで通知しているようにしているのですが、トピックへのメールアドレスのサブスクライブは手作業で行っています。Terraform では SNS トピックへのメールアドレスのサブスクライブはできないためです。

Lambda 経由で Slack とかにした方が良いかも。

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?