(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【AWS/Cost Explorer】CLIによる通知付きBudgetの作り方及び通知設定の確認方法

スポンサーリンク

やりたいこと

AWSコマンドで予算を管理するバジェットを作成し、作成したバジェットを確認する。

環境情報
 aws --version
aws-cli/1.19.112 Python/2.7.18 Linux/4.14.322-244.536.amzn2.x86_64 botocore/1.20.112

やり方

作成方法は以下。

$ aws budgets create-budget --account-id xx --budget file://a.json --notifications-with-subscribers file://b.json

アカウントIDは以下で確認を行い

$ aws sts get-caller-identity
{...
    "Account": "xx",
...}

バジェットと通知設定は以下のようなファイルを事前準備しておく。

        {
            "BudgetName": "xx",
            "BudgetLimit": {
                "Amount": "5.0",
                "Unit": "USD"
            },
            "CostFilters": {
                "TagKeyValue": [
                    "user:Owner$xx"
                ]
            },
            "CostTypes": {
                "IncludeTax": true,
                "IncludeSubscription": true,
                "UseBlended": false,
                "IncludeRefund": false,
                "IncludeCredit": false,
                "IncludeUpfront": true,
                "IncludeRecurring": true,
                "IncludeOtherSubscription": true,
                "IncludeSupport": true,
                "IncludeDiscount": true,
                "UseAmortized": false
            },
            "TimeUnit": "ANNUALLY",
            "TimePeriod": {
                "Start": "2023-01-01T00:00:00+00:00",
                "End": "2083-01-01T00:00:00+00:00"
            },
            "BudgetType": "COST",
        }
    ]
}
[
{
 "Notification": 
        {
            "NotificationType": "ACTUAL",
            "ComparisonOperator": "GREATER_THAN",
            "Threshold": 100.0
        },
        "Subscribers":[
                {
                           "SubscriptionType": "EMAIL",
                           "Address": "xx"}
        ]
}
]

作成した通知情報の確認は以下にて該当のバジェット名を確認した後に

$ aws budgets describe-budgets --account-id xx | grep Name
            "BudgetName": "xx",

同バジェット名を引数に通知情報を確認する。

$ aws budgets describe-notifications-for-budget --account-id xx --budget-name "xx"
{
    "Notifications": [
        {
            "NotificationType": "ACTUAL",
            "ComparisonOperator": "GREATER_THAN",
            "Threshold": 50.0,
            "NotificationState": "OK"
        }
    ]
}

尚、通知先は一度以下のようにファイルを出力した後に

$ aws budgets describe-notifications-for-budget --account-id xx --budget-name "xx" --query Notifications[0] > c.json

以下のようにファイルを指定すれば確認できる。

$ aws budgets describe-subscribers-for-notification --account-id xx --budget-name "xx" --notification file://c.json
{
    "Subscribers": [
        {
            "SubscriptionType": "EMAIL",
            "Address": "xx"
        }
    ]
}

以下、補足です。

補足

今回はコストフィルターとしてOwnerというタグで抽出をおこなっていますが下記を抜けばアカウント全体のコストを監視できます。

            "CostFilters": {
                "TagKeyValue": [
                    "user:Owner$xx"
                ]
            },

また、バジェットの利用状況に基づく通知は以下のように記述することで複数の条件の記載が可能です。

{
 "Notification": 
...
},
{
 "Notification": 
}
]

以上。