

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# AWS Budgets 使用 的範例 AWS CLI
<a name="cli_budgets_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 來執行動作和實作常見案例 AWS Budgets。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `create-budget`
<a name="budgets_CreateBudget_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-budget`。

**AWS CLI**  
**建立成本和用量預算**  
下列 `create-budget` 命令會建立成本和用量預算。  

```
aws budgets create-budget \
    --account-id 111122223333 \
    --budget file://budget.json \
    --notifications-with-subscribers file://notifications-with-subscribers.json
```
`budget.json` 的內容：  

```
{
    "BudgetLimit": {
        "Amount": "100",
        "Unit": "USD"
    },
    "BudgetName": "Example Tag Budget",
    "BudgetType": "COST",
    "CostFilters": {
        "TagKeyValue": [
            "user:Key$value1",
            "user:Key$value2"
        ]
    },
    "CostTypes": {
        "IncludeCredit": true,
        "IncludeDiscount": true,
        "IncludeOtherSubscription": true,
        "IncludeRecurring": true,
        "IncludeRefund": true,
        "IncludeSubscription": true,
        "IncludeSupport": true,
        "IncludeTax": true,
        "IncludeUpfront": true,
        "UseBlended": false
    },
    "TimePeriod": {
        "Start": 1477958399,
        "End": 3706473600
    },
    "TimeUnit": "MONTHLY"
}
```
`notifications-with-subscribers.json` 的內容：  

```
[
    {
        "Notification": {
            "ComparisonOperator": "GREATER_THAN",
            "NotificationType": "ACTUAL",
            "Threshold": 80,
            "ThresholdType": "PERCENTAGE"
        },
        "Subscribers": [
            {
                "Address": "example@example.com",
                "SubscriptionType": "EMAIL"
            }
        ]
    }
]
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateBudget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/create-budget.html)。

### `create-notification`
<a name="budgets_CreateNotification_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-notification`。

**AWS CLI**  
**建立指定的成本和用量預算通知**  
此範例會建立指定的成本和用量預算通知。  
命令：  

```
aws budgets create-notification --account-id 111122223333 --budget-name "Example Budget" --notification NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE --subscriber SubscriptionType=EMAIL,Address=example@example.com
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateNotification](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/create-notification.html)。

### `create-subscriber`
<a name="budgets_CreateSubscriber_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-subscriber`。

**AWS CLI**  
**建立適用與成本和用量預算相關聯通知的訂閱用戶**  
此範例會建立適用於指定通知的訂閱用戶。  
命令：  

```
aws budgets create-subscriber --account-id 111122223333 --budget-name "Example Budget" --notification NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE --subscriber SubscriptionType=EMAIL,Address=example@example.com
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateSubscriber](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/create-subscriber.html)。

### `delete-budget`
<a name="budgets_DeleteBudget_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-budget`。

**AWS CLI**  
**刪除成本和用量預算**  
此範例會刪除指定的成本和用量預算。  
命令：  

```
aws budgets delete-budget --account-id 111122223333 --budget-name "Example Budget"
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteBudget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/delete-budget.html)。

### `delete-notification`
<a name="budgets_DeleteNotification_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-notification`。

**AWS CLI**  
**刪除預算通知**  
此範例會刪除指定預算的指定通知。  
命令：  

```
aws budgets delete-notification --account-id 111122223333 --budget-name "Example Budget" --notification NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteNotification](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/delete-notification.html)。

### `delete-subscriber`
<a name="budgets_DeleteSubscriber_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-subscriber`。

**AWS CLI**  
**將訂閱用戶從通知刪除**  
此範例會從指定的通知刪除指定的訂閱用戶。  
命令：  

```
aws budgets delete-subscriber --account-id 111122223333 --budget-name "Example Budget" --notification NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE --subscriber SubscriptionType=EMAIL,Address=example@example.com
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteSubscriber](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/delete-subscriber.html)。

### `describe-budget`
<a name="budgets_DescribeBudget_cli_topic"></a>

以下程式碼範例顯示如何使用 `describe-budget`。

**AWS CLI**  
**擷取與帳戶相關聯的預算**  
此範例會擷取指定的成本和用量預算。  
命令：  

```
aws budgets describe-budget --account-id 111122223333 --budget-name "Example Budget"
```
輸出：  

```
{
   "Budget": {
       "CalculatedSpend": {
           "ForecastedSpend": {
               "Amount": "2641.54800000000022919266484677791595458984375",
               "Unit": "USD"
           },
           "ActualSpend": {
               "Amount": "604.4560000000000172803993336856365203857421875",
               "Unit": "USD"
           }
       },
       "BudgetType": "COST",
       "BudgetLimit": {
           "Amount": "100",
           "Unit": "USD"
       },
       "BudgetName": "Example Budget",
       "CostTypes": {
           "IncludeOtherSubscription": true,
           "IncludeUpfront": true,
           "IncludeRefund": true,
           "UseBlended": false,
           "IncludeDiscount": true,
           "UseAmortized": false,
           "IncludeTax": true,
           "IncludeCredit": true,
           "IncludeSupport": true,
           "IncludeRecurring": true,
           "IncludeSubscription": true
       },
       "TimeUnit": "MONTHLY",
       "TimePeriod": {
           "Start": 1477958399.0,
           "End": 3706473600.0
       },
       "CostFilters": {
           "AZ": [
               "us-east-1"
           ]
       }
   }
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeBudget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/describe-budget.html)。

### `describe-budgets`
<a name="budgets_DescribeBudgets_cli_topic"></a>

以下程式碼範例顯示如何使用 `describe-budgets`。

**AWS CLI**  
**擷取與帳戶關聯的預算**  
此範例會擷取帳戶的成本和用量預算。  
命令：  

```
aws budgets describe-budgets --account-id 111122223333 --max-results 20
```
輸出：  

```
{
   "Budgets": [
       {
           "CalculatedSpend": {
               "ForecastedSpend": {
                   "Amount": "2641.54800000000022919266484677791595458984375",
                   "Unit": "USD"
               },
               "ActualSpend": {
                   "Amount": "604.4560000000000172803993336856365203857421875",
                   "Unit": "USD"
               }
           },
           "BudgetType": "COST",
           "BudgetLimit": {
               "Amount": "100",
               "Unit": "USD"
           },
           "BudgetName": "Example Budget",
           "CostTypes": {
               "IncludeOtherSubscription": true,
               "IncludeUpfront": true,
               "IncludeRefund": true,
               "UseBlended": false,
               "IncludeDiscount": true,
               "UseAmortized": false,
               "IncludeTax": true,
               "IncludeCredit": true,
               "IncludeSupport": true,
               "IncludeRecurring": true,
               "IncludeSubscription": true
           },
           "TimeUnit": "MONTHLY",
           "TimePeriod": {
               "Start": 1477958399.0,
               "End": 3706473600.0
           },
           "CostFilters": {
               "AZ": [
                   "us-east-1"
               ]
           }
       }
   ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeBudgets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/describe-budgets.html)。

### `describe-notifications-for-budget`
<a name="budgets_DescribeNotificationsForBudget_cli_topic"></a>

以下程式碼範例顯示如何使用 `describe-notifications-for-budget`。

**AWS CLI**  
**擷取預算的通知**  
此範例會擷取成本和用量預算的通知。  
命令：  

```
aws budgets describe-notifications-for-budget --account-id 111122223333 --budget-name "Example Budget" --max-results 5
```
輸出：  

```
{
   "Notifications": [
       {
           "Threshold": 80.0,
           "ComparisonOperator": "GREATER_THAN",
           "NotificationType": "ACTUAL"
       }
   ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeNotificationsForBudget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/describe-notifications-for-budget.html)。

### `describe-subscribers-for-notification`
<a name="budgets_DescribeSubscribersForNotification_cli_topic"></a>

以下程式碼範例顯示如何使用 `describe-subscribers-for-notification`。

**AWS CLI**  
**擷取預算通知的訂閱用戶。**  
此範例會擷取成本和用量預算通知的訂閱用戶。  
命令：  

```
aws budgets describe-subscribers-for-notification --account-id 111122223333 --budget-name "Example Budget" --notification NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE --max-results 5
```
輸出：  

```
{
   "Subscribers": [
       {
           "SubscriptionType": "EMAIL",
           "Address": "example2@example.com"
       },
       {
           "SubscriptionType": "EMAIL",
           "Address": "example@example.com"
       }
   ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeSubscribersForNotification](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/describe-subscribers-for-notification.html)。

### `update-budget`
<a name="budgets_UpdateBudget_cli_topic"></a>

以下程式碼範例顯示如何使用 `update-budget`。

**AWS CLI**  
**取代成本和用量預算的預算**  
此範例將成本和用量預算取代為新預算。  
命令：  

```
aws budgets update-budget --account-id 111122223333 --new-budget file://new-budget.json
```
new-budget.json：  

```
{
    "BudgetLimit": {
       "Amount": "100",
       "Unit": "USD"
    },
    "BudgetName": "Example Budget",
    "BudgetType": "COST",
    "CostFilters": {
       "AZ" : [ "us-east-1" ]
    },
    "CostTypes": {
       "IncludeCredit": false,
       "IncludeDiscount": true,
       "IncludeOtherSubscription": true,
       "IncludeRecurring": true,
       "IncludeRefund": true,
       "IncludeSubscription": true,
       "IncludeSupport": true,
       "IncludeTax": true,
       "IncludeUpfront": true,
       "UseBlended": false,
       "UseAmortized": true
    },
    "TimePeriod": {
       "Start": 1477958399,
       "End": 3706473600
    },
    "TimeUnit": "MONTHLY"
 }
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateBudget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/update-budget.html)。

### `update-notification`
<a name="budgets_UpdateNotification_cli_topic"></a>

以下程式碼範例顯示如何使用 `update-notification`。

**AWS CLI**  
**取代成本和用量預算的通知**  
此範例會以 90% 的成本和用量預算通知，取代 80% 通知。  
命令：  

```
aws budgets update-notification --account-id 111122223333 --budget-name "Example Budget" --old-notification  NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE --new-notification  NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=90,ThresholdType=PERCENTAGE
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateNotification](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/update-notification.html)。

### `update-subscriber`
<a name="budgets_UpdateSubscriber_cli_topic"></a>

以下程式碼範例顯示如何使用 `update-subscriber`。

**AWS CLI**  
**取代成本和用量預算的訂閱用戶**  
此範例會取代成本和用量預算的訂閱用戶。  
命令：  

```
aws budgets update-subscriber --account-id 111122223333 --budget-name "Example Budget" --notification NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE --old-subscriber SubscriptionType=EMAIL,Address=example@example.com --new-subscriber SubscriptionType=EMAIL,Address=example2@example.com
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateSubscriber](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/budgets/update-subscriber.html)。