

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# AWS Budgets 使用 AWS SDKs程式碼範例
<a name="budgets_code_examples"></a>

下列程式碼範例示範如何使用 AWS Budgets 搭配 AWS 軟體開發套件 (SDK)。

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

**其他資源**
+  **[AWS Budgets 開發人員指南](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html)** – 有關 的詳細資訊 AWS Budgets。
+ **[AWS Budgets API 參考](https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/Welcome.html)** – 所有可用 AWS Budgets 動作的詳細資訊。
+ **[AWS 開發人員中心](https://aws.amazon.com/developer/code-examples/?awsf.sdk-code-examples-product=product%23)** – 您可以依類別或全文搜尋篩選的程式碼範例。
+ **[AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)** – GitHub 儲存庫使用慣用語言的完整程式碼。包含設定和執行程式碼的指示。

**Contents**
+ [基本概念](budgets_code_examples_basics.md)
  + [動作](budgets_code_examples_actions.md)
    + [`CreateBudget`](budgets_example_budgets_CreateBudget_section.md)

# AWS Budgets 使用 AWS SDKs的基本範例
<a name="budgets_code_examples_basics"></a>

下列程式碼範例示範如何 AWS Budgets 搭配 AWS SDKs 使用 的基本概念。

**Contents**
+ [動作](budgets_code_examples_actions.md)
  + [`CreateBudget`](budgets_example_budgets_CreateBudget_section.md)

# AWS Budgets 使用 AWS SDKs的動作
<a name="budgets_code_examples_actions"></a>

下列程式碼範例示範如何使用 AWS SDKs執行個別 AWS Budgets 動作。每個範例均包含 GitHub 的連結，您可以在連結中找到設定和執行程式碼的相關說明。

 下列範例僅包含最常使用的動作。如需完整清單，請參閱《[AWS Budgets API 參考](https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/Welcome.html)》。

**Topics**
+ [`CreateBudget`](budgets_example_budgets_CreateBudget_section.md)

# 搭配使用 `CreateBudget` 與 CLI
<a name="budgets_example_budgets_CreateBudget_section"></a>

下列程式碼範例示範如何使用 `CreateBudget`。

------
#### [ CLI ]

**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)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：透過電子郵件通知，建立具有指定預算和時間限制的新預算。**  

```
$notification = @{
    NotificationType = "ACTUAL"
    ComparisonOperator = "GREATER_THAN"
    Threshold = 80
}

$addressObject = @{
    Address = @("user@domain.com")
    SubscriptionType = "EMAIL"
}

$subscriber = New-Object Amazon.Budgets.Model.NotificationWithSubscribers
$subscriber.Notification = $notification
$subscriber.Subscribers.Add($addressObject)

$startDate = [datetime]::new(2017,09,25)
$endDate = [datetime]::new(2017,10,25)

New-BGTBudget -Budget_BudgetName "Tester" -Budget_BudgetType COST -CostTypes_IncludeTax $true -Budget_TimeUnit MONTHLY -BudgetLimit_Unit USD -TimePeriod_Start $startDate -TimePeriod_End $endDate -AccountId 123456789012 -BudgetLimit_Amount 200 -NotificationsWithSubscriber $subscriber
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CreateBudget](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：透過電子郵件通知，建立具有指定預算和時間限制的新預算。**  

```
$notification = @{
    NotificationType = "ACTUAL"
    ComparisonOperator = "GREATER_THAN"
    Threshold = 80
}

$addressObject = @{
    Address = @("user@domain.com")
    SubscriptionType = "EMAIL"
}

$subscriber = New-Object Amazon.Budgets.Model.NotificationWithSubscribers
$subscriber.Notification = $notification
$subscriber.Subscribers.Add($addressObject)

$startDate = [datetime]::new(2017,09,25)
$endDate = [datetime]::new(2017,10,25)

New-BGTBudget -Budget_BudgetName "Tester" -Budget_BudgetType COST -CostTypes_IncludeTax $true -Budget_TimeUnit MONTHLY -BudgetLimit_Unit USD -TimePeriod_Start $startDate -TimePeriod_End $endDate -AccountId 123456789012 -BudgetLimit_Amount 200 -NotificationsWithSubscriber $subscriber
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateBudget](https://docs.aws.amazon.com/powershell/v5/reference)。

------