0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS 請求アラートを CloudFormation で試してみた!

Posted at

はじめに

AWS 検証環境で EC2 が起動しっぱなしとなっていて、請求料金が規定値(例えば予算1万円以内とか)を超えることが度々・・・
規定値になったらメールでお知らせしてくれるとウレシイなと思い、調べてみました。

仕様

規定値(予算)を指定し、規定値の○%(3段階)に達したらアラートメールを送付する仕様です。

YAMLテンプレート

下記参考サイトのパラメータ入力順番だけ変更しました。

details billing-alarm.yaml(折り畳んでます)
AWSTemplateFormatVersion: '2010-09-09'
Description: "Set & Update Budget Alarts"

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Resource Configuration
        Parameters:
          - Amount
          - NotificationEmail
          - ThresholdLow
          - ThresholdMid
          - ThresholdHigh

Parameters:
  Amount:
    Type: Number
    Default: 1
    Description: Budget Limit Amount

  NotificationEmail:
    Type: String
    Description: Notification Email Address

  ThresholdHigh:
    Type: Number
    Default: 150
    MinValue: 50
    MaxValue: 200
    Description: 50-200 % of budgeted amount(High)

  ThresholdMid:
    Type: Number
    Default: 100
    MinValue: 50
    MaxValue: 200
    Description: 50-200 % of budgeted amount(Middle)

  ThresholdLow:
    Type: Number
    Default: 80
    MinValue: 5
    MaxValue: 200
    Description: 50-200 % of budgeted amount(Low)

Resources:
  BudgetAlerts:
    Type: AWS::Budgets::Budget
    Properties:
      Budget:
        BudgetLimit:
          Amount: !Ref Amount
          Unit: USD
        TimeUnit: MONTHLY
        BudgetType: COST
        BudgetName: !Sub ${AWS::StackName}-billing-alarm
      NotificationsWithSubscribers:
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref ThresholdHigh
          Subscribers:
            - SubscriptionType: EMAIL
              Address: !Ref NotificationEmail
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref ThresholdMid
          Subscribers:
            - SubscriptionType: EMAIL
              Address: !Ref NotificationEmail
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref ThresholdLow
            ThresholdType: PERCENTAGE
          Subscribers:
            - SubscriptionType: EMAIL
              Address: !Ref NotificationEmail
      ResourceTags:
        - Key: "CloudFormation"
          Value: !Ref AWS::StackName

YAMLパラメーターの解説

パラメーター名 解説 備考
Amount 予算を設定(USD) 1 万円の場合、執筆時点では約 65 USD
NotificationEmail 通知先メールアドレス
ThresholdLow 予算の○%になったらアラート 初期値:80%
ThresholdMid 予算の○%になったらアラート 初期値:100%
ThresholdHigh 予算の○%になったらアラート 初期値:150%

動作確認

予算の各基準値に達した際、YAML テンプレート内の BudgetName で指定したタイトルのメールが指定メールアドレスに届きます。
※初回は基準値を低めにして試すのがおすすめです。

注意

  • どのサービスが課金されてるかは別途管理コンソールで確認してください。
  • 基準値に達する毎にアラートメールが送付されます。
    短時間に請求金額が上昇した場合は立て続けにアラートメールが送付されます。
  • 基準値は月ごとに有効です。
    同月で1度 ThresholdHigh (MAX基準値)を超過した場合は以降アラートメールは送付されません。
    翌月になれば、クリアされます。

まとめ

検証などで AWS を利用する場合、予算内で・・・とかあると思います。
そういった場合に活用できるかなと思いますので、参考にしてみてください!
この記事が、誰かの役に立てば幸いです。

参考サイト

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?