LoginSignup
0
0

More than 1 year has passed since last update.

AWS Budgetsの予算超過アラートを構築ハンズオン

Posted at

はじめに

昨年は部内で利用できるAWS検証環境を超過してしまったので、月々の予算に敏感になるためにも、今まで利用しなかったAWS Budgetsを利用していこうと思い立ち構築を考えました。
まずは後続の処理はSNSで通知するだけの、スモールステップで構築します。


構成図


ハンズオン

構築のながれ

1.SNS作成:予算超過通知が送られる先の構築

2.AWS Budgets作成:予算・予算に対するアラートを構築


1.SNS作成:予算超過通知が送られる先の構築

1.1 SNSを構築する

署名付きURLを通知する、SNSトピックを構築します。
TopicPolicy部分は、検証のため制限していません。

AWSTemplateFormatVersion: "2010-09-09"
Description: SNS Create

# ------------------------------------------------------------#
#  Metadata
# ------------------------------------------------------------#
Metadata:
  "AWS::CloudFormation::Interface":
    ParameterGroups:
      - Label:
          default: "SNS Configuration"
        Parameters:
        - TopicName
        - Endpoint
        - TagsValueUserName

    ParameterLabels:
      TopicName:
        default: "TopicName"
      Endpoint:
        default: "MailAddress"
      Endpoint:
        default: "UserName"

# ------------------------------------------------------------#
#  InputParameters
# ------------------------------------------------------------#
Parameters:
  TopicName:
    Type: String
    Default: "【各自TopicName】"
  Endpoint:
    Type: String
    Default: "【各自メールアドレス】"
  TagsValueUserName:
    Type: String
    Default: "【各自タグ名】"

# ------------------------------------------------------------#
#  Resources
# ------------------------------------------------------------#
Resources:
  SNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: !Ref TopicName
      Subscription:
        - Endpoint: !Ref Endpoint
          Protocol: email
      Tags:
        - Key: "User"
          Value: !Ref TagsValueUserName    

  TopicPolicy:
    Type: AWS::SNS::TopicPolicy
    Properties:
      Topics:
        - !Ref SNSTopic
      PolicyDocument:
        Id: !Ref SNSTopic
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              AWS: "*"
            Action: SNS:Publish
            Resource: !Ref SNSTopic

# ------------------------------------------------------------#
# Output Parameters
# ------------------------------------------------------------#                
Outputs:
  SNSArn:
    Value: !Ref SNSTopic
    Export:
      Name: !Sub "${TopicName}-arn"
  SNSTopicName:
    Value: !Ref TopicName
    Export:
      Name: !Ref TopicName

1.2 上記設定後に、SNSから送られてきたメールのサブスクリプションを押下する

①送られてきたメールの赤枠部分をクリックする

②画面が遷移して下記画面が表示されると、サブスクリプションが開始される

※上記青枠部分をクリックすると、サブスクリプションが解除される


2.AWS Budgets作成:予算・予算に対するアラートを構築

Ammountに全体の予算金額を入力
ThrshouldHighは 予算を100%を超過した場合、メール通知をする
ThrshouldMidは 予算を80%を超過した場合、メール通知をする
ThrshouldMidは 予算を50%を超過した場合、メール通知をする

AWSTemplateFormatVersion: '2010-09-09'
Description:
  Create Budgets
# ------------------------------------------------------------#
#  Metadata
# ------------------------------------------------------------#
Metadata:
  "AWS::CloudFormation::Interface":
    ParameterGroups:
      - Label:
          default: "AWS Budget Configuration"
        Parameters:
          - BudgetName
          - Ammount
          - ThreshouldHigh
          - ThreshouldMid
          - ThreshouldLow

# ------------------------------------------------------------#
#  InputParameters
# ------------------------------------------------------------#
Parameters:
  BudgetName:
    Type: String
    Default: 【各自設定したい予算書の名前】
    Description: Budget Name
  Ammount:
    Type: String
    Default: 【各自設定したい予算金額】
    Description: Budget Limit Amount
  ThreshouldHigh:
    Type: Number
    Default: 100
    MinValue: 50
    MaxValue: 100
    Description: 50-100 % of budgeted AmountHigh
  ThreshouldMid:
    Type: Number
    Default: 80
    MinValue: 50
    MaxValue: 100
    Description: 50-100 % of budgeted AmountMiddle
  ThreshouldLow:
    Type: Number
    Default: 50
    MinValue: 5
    MaxValue: 100
    Description: 50-100 % of budgeted AmountLow

# ------------------------------------------------------------#
#  Resources
# ------------------------------------------------------------#
Resources:
# ------------------------------------------------------------#
#  BudgetAlarts
# ------------------------------------------------------------#    
  BudgetAlarts:
    Type: "AWS::Budgets::Budget"
    Properties:
      Budget:
        BudgetName: !Ref BudgetName
        BudgetLimit:
          Amount: !Ref Ammount
          Unit: USD
        TimeUnit: MONTHLY
        BudgetType: COST
      NotificationsWithSubscribers:
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref ThreshouldHigh
          Subscribers:
            - SubscriptionType: SNS
              Address: !ImportValue cfn-sns-topic-inamura-arn
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref ThreshouldMid
          Subscribers:
          - SubscriptionType: SNS
            Address: !ImportValue cfn-sns-topic-inamura-arn
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: !Ref ThreshouldLow
          Subscribers:
          - SubscriptionType: SNS
            Address: !ImportValue cfn-sns-topic-inamura-arn

挙動の確認

予算に対して50,80,100%を超過すると、SNSが通知される

設定した時点で50,80,100%を超過していた場合それぞれメール通知が届く(※100%の場合ならば、50%のメール、80%のメール、100%のメールと3通受信)



さいごに

さくっと構築できましたが、これではSNSで登録している自分にしか通知がこないので、Lambdaを挟み込んで部署内で利用しているチャットツールに連絡をするなど運用的には考えなければいけなさそうです。

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