AWS CLIで予算管理機能を設定する

予算管理機能がアップデートされました。

[New AWS Budgets features include API access, usage-based budgets, and more!](https://aws.amazon.com/about-aws/whats-new/2016/10/new-aws-budgets-features-include-api-access-usage-based-budgets-and-more/)


CLIでも使えるようなので使ってみました。

ぶっちゃけ、Management Consoleで設定した方が楽です。

[budgets](http://docs.aws.amazon.com/cli/latest/reference/budgets/index.html)


# 予算の定義

以下のように予算を定義します。

```bash:コマンド
BUDGET_NAME="budget"
BUDGET_TYPE="COST"
AMOUNT="100"
UNIT="USD"
INCLUDE_TAX=false
INCLUDE_SUBSCRIPTION=true
USE_BLENDED=true
TIME_UNIT="MONTHLY"
TIME_PERIOD_START=$(date -d '2017/1/1 00:00:00' +'%s') && echo ${TIME_PERIOD_START}
TIME_PERIOD_END=$(date -d '2018/1/1 00:00:00' +'%s') && echo ${TIME_PERIOD_END}

NOTIFICATION_TYPE="ACTUAL"
COMPARISON_OPERATOR="GREATER_THAN"
THRESHOLD="80.0"

SUBSCRIPTION_TYPE="EMAIL"
ADDRESS="user@example.com"
```

```bash:コマンド
cat << EOF > budget.json
{
  "BudgetName": "${BUDGET_NAME}",
  "BudgetLimit": {
    "Amount": "${AMOUNT}",
    "Unit": "${UNIT}"
  },
  "CostTypes": {
    "IncludeTax": ${INCLUDE_TAX},
    "IncludeSubscription": ${INCLUDE_SUBSCRIPTION},
    "UseBlended": ${USE_BLENDED}
  },
  "TimeUnit": "${TIME_UNIT}",
  "TimePeriod": {
    "Start": "${TIME_PERIOD_START}",
    "End": "${TIME_PERIOD_END}"
  },
  "BudgetType": "${BUDGET_TYPE}"
}
EOF

cat budget.json
```

```bash:コマンド
jsonlint -q budget.json
```

```bash:コマンド
cat << EOF > notifications-with-subscribers.json
[
  {
    "Notification": {
      "NotificationType": "${NOTIFICATION_TYPE}",
      "ComparisonOperator": "${COMPARISON_OPERATOR}",
      "Threshold": ${THRESHOLD}
    },
    "Subscribers": [
      {
        "SubscriptionType": "${SUBSCRIPTION_TYPE}",
        "Address": "${ADDRESS}"
      }
    ]
  }
]
EOF

cat notifications-with-subscribers.json
```

```bash:コマンド
jsonlint -q notifications-with-subscribers.json
```


# 予算の作成

```bash:コマンド
AWS_ID=$(aws sts get-caller-identity \
    --query Account \
    --output text) \
    && echo ${AWS_ID}
```

```bash:コマンド
aws budgets create-budget \
    --account-id ${AWS_ID}\
    --budget file://budget.json \
    --notifications-with-subscribers file://notifications-with-subscribers.json
```


# 予算の確認

```bash:コマンド
aws budgets describe-budgets \
    --account-id ${AWS_ID}
```

```json:結果
{
    "Budgets": [
        {
            "BudgetType": "COST",
            "BudgetLimit": {
                "Amount": "100",
                "Unit": "USD"
            },
            "CostTypes": {
                "IncludeTax": false,
                "UseBlended": true,
                "IncludeSubscription": true
            },
            "BudgetName": "budget",
            "CalculatedSpend": {},
            "TimeUnit": "MONTHLY",
            "TimePeriod": {
                "Start": 1483228800.0,
                "End": 1514764800.0
            }
        }
    ]
}
```

```bash:コマンド
aws budgets describe-budget \
    --account-id ${AWS_ID} \
    --budget-name ${BUDGET_NAME}
```

```json:結果
{
    "Budget": {
        "BudgetType": "COST",
        "BudgetLimit": {
            "Amount": "100",
            "Unit": "USD"
        },
        "CostTypes": {
            "IncludeTax": false,
            "UseBlended": true,
            "IncludeSubscription": true
        },
        "BudgetName": "budget",
        "CalculatedSpend": {},
        "TimeUnit": "MONTHLY",
        "TimePeriod": {
            "Start": 1483228800.0,
            "End": 1514764800.0
        }
    }
}
```

```bash:コマンド
aws budgets describe-notifications-for-budget \
    --account-id ${AWS_ID} \
    --budget-name ${BUDGET_NAME} \
    --query Notifications[0] \
    > notification.json \
    && cat notification.json
```

```json:結果
{
    "Threshold": 80.0,
    "ComparisonOperator": "GREATER_THAN",
    "NotificationType": "ACTUAL"
}
```

```bash:コマンド
aws budgets describe-subscribers-for-notification \
    --account-id ${AWS_ID}\
    --budget-name ${BUDGET_NAME}\
    --notification file://notification.json
```

```json:結果
{
    "Subscribers": [
        {
            "SubscriptionType": "EMAIL",
            "Address": "user@example.com"
        }
    ]
}
```