LoginSignup
4
2

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-11-14

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

New AWS Budgets features include API access, usage-based budgets, and more!

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

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

budgets

予算の定義

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

コマンド
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"
コマンド
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
コマンド
jsonlint -q budget.json
コマンド
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
コマンド
jsonlint -q notifications-with-subscribers.json

予算の作成

コマンド
AWS_ID=$(aws sts get-caller-identity \
    --query Account \
    --output text) \
    && echo ${AWS_ID}
コマンド
aws budgets create-budget \
    --account-id ${AWS_ID}\
    --budget file://budget.json \
    --notifications-with-subscribers file://notifications-with-subscribers.json

予算の確認

コマンド
aws budgets describe-budgets \
    --account-id ${AWS_ID}
結果
{
    "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
            }
        }
    ]
}
コマンド
aws budgets describe-budget \
    --account-id ${AWS_ID} \
    --budget-name ${BUDGET_NAME}
結果
{
    "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
        }
    }
}
コマンド
aws budgets describe-notifications-for-budget \
    --account-id ${AWS_ID} \
    --budget-name ${BUDGET_NAME} \
    --query Notifications[0] \
    > notification.json \
    && cat notification.json
結果
{
    "Threshold": 80.0,
    "ComparisonOperator": "GREATER_THAN",
    "NotificationType": "ACTUAL"
}
コマンド
aws budgets describe-subscribers-for-notification \
    --account-id ${AWS_ID}\
    --budget-name ${BUDGET_NAME}\
    --notification file://notification.json
結果
{
    "Subscribers": [
        {
            "SubscriptionType": "EMAIL",
            "Address": "user@example.com"
        }
    ]
}
4
2
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
4
2