

# 使用 AWS CLI 的 Cost Explorer Service 示例
<a name="cli_cost-explorer_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 Cost Explorer Service 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

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

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

### `get-cost-and-usage`
<a name="cost-explorer_GetCostAndUsage_cli_topic"></a>

以下代码示例演示了如何使用 `get-cost-and-usage`。

**AWS CLI**  
**检索账户在 2017 年 9 月的 S3 使用情况**  
以下 `get-cost-and-usage` 示例检索账户在 2017 年 9 月的 S3 使用情况。  

```
aws ce get-cost-and-usage \
    --time-period Start=2017-09-01,End=2017-10-01 \
    --granularity MONTHLY \
    --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" \
    --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment \
    --filter file://filters.json
```
 的内容`filters.json`：  

```
{
    "Dimensions": {
        "Key": "SERVICE",
        "Values": [
            "Amazon Simple Storage Service"
        ]
    }
}
```
输出：  

```
{
    "GroupDefinitions": [
        {
            "Type": "DIMENSION",
            "Key": "SERVICE"
        },
        {
            "Type": "TAG",
            "Key": "Environment"
        }
    ],
    "ResultsByTime": [
        {
            "Estimated": false,
            "TimePeriod": {
                "Start": "2017-09-01",
                "End": "2017-10-01"
            },
            "Total": {},
            "Groups": [
                {
                    "Keys": [
                        "Amazon Simple Storage Service",
                        "Environment$"
                    ],
                    "Metrics": {
                        "BlendedCost": {
                            "Amount": "40.3527508453",
                            "Unit": "USD"
                        },
                        "UnblendedCost": {
                            "Amount": "40.3543773134",
                            "Unit": "USD"
                        },
                        "UsageQuantity": {
                            "Amount": "9312771.098461578",
                            "Unit": "N/A"
                        }
                    }
                },
                {
                    "Keys": [
                        "Amazon Simple Storage Service",
                        "Environment$Dev"
                    ],
                    "Metrics": {
                        "BlendedCost": {
                            "Amount": "0.2682364644",
                            "Unit": "USD"
                        },
                        "UnblendedCost": {
                            "Amount": "0.2682364644",
                            "Unit": "USD"
                        },
                        "UsageQuantity": {
                            "Amount": "22403.4395271182",
                            "Unit": "N/A"
                        }
                    }
                }
            ]
        }
    ]
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetCostAndUsage](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-cost-and-usage.html)。

### `get-dimension-values`
<a name="cost-explorer_GetDimensionValues_cli_topic"></a>

以下代码示例演示了如何使用 `get-dimension-values`。

**AWS CLI**  
**检索值为“Elastic”的维度 SERVICE 的标签**  
此示例检索 2017 年 1 月 1 日至 2017 年 5 月 18 日的值为“Elastic”的维度 SERVICE 标签。  
命令:  

```
aws ce get-dimension-values --search-string Elastic --time-period Start=2017-01-01,End=2017-05-18 --dimension SERVICE
```
输出：  

```
{
   "TotalSize": 6,
   "DimensionValues": [
       {
           "Attributes": {},
           "Value": "Amazon ElastiCache"
       },
       {
           "Attributes": {},
           "Value": "EC2 - Other"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elastic Compute Cloud - Compute"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elastic Load Balancing"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elastic MapReduce"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elasticsearch Service"
       }
   ],
   "ReturnSize": 6
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetDimensionValues](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-dimension-values.html)。

### `get-reservation-coverage`
<a name="cost-explorer_GetReservationCoverage_cli_topic"></a>

以下代码示例演示了如何使用 `get-reservation-coverage`。

**AWS CLI**  
**检索 us-east-1 区域中 EC2 t2.nano 实例的预留覆盖范围**  
此示例检索 2017 年 7 月至 9 月 us-east-1 区域中 EC2 t2.nano 实例的预留覆盖范围。  
命令:  

```
aws ce get-reservation-coverage --time-period Start=2017-07-01,End=2017-10-01 --group-by Type=Dimension,Key=REGION --filter file://filters.json
```
filters.json：  

```
{
   "And": [
     {
       "Dimensions": {
         "Key": "INSTANCE_TYPE",
         "Values": [
           "t2.nano"
         ]
       },
       "Dimensions": {
         "Key": "REGION",
         "Values": [
           "us-east-1"
         ]
       }
     }
   ]
 }
```
输出：  

```
{
   "TotalSize": 6,
   "DimensionValues": [
       {
           "Attributes": {},
           "Value": "Amazon ElastiCache"
       },
       {
           "Attributes": {},
           "Value": "EC2 - Other"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elastic Compute Cloud - Compute"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elastic Load Balancing"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elastic MapReduce"
       },
       {
           "Attributes": {},
           "Value": "Amazon Elasticsearch Service"
       }
   ],
   "ReturnSize": 6
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetReservationCoverage](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-reservation-coverage.html)。

### `get-reservation-purchase-recommendation`
<a name="cost-explorer_GetReservationPurchaseRecommendation_cli_topic"></a>

以下代码示例演示了如何使用 `get-reservation-purchase-recommendation`。

**AWS CLI**  
**检索为期三年的部分预付 EC2 RI 的预留建议**  
以下 `get-reservation-purchase-recommendation` 示例根据过去 60 天的 EC2 使用情况，检索为期三年的部分预付 EC2 实例的建议。  

```
aws ce get-reservation-purchase-recommendation \
    --service "Amazon Redshift" \
    --lookback-period-in-days SIXTY_DAYS \
    --term-in-years THREE_YEARS \
    --payment-option PARTIAL_UPFRONT
```
输出：  

```
{
    "Recommendations": [],
    "Metadata": {
        "GenerationTimestamp": "2018-08-08T15:20:57Z",
        "RecommendationId": "00d59dde-a1ad-473f-8ff2-iexample3330b"
    }
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetReservationPurchaseRecommendation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-reservation-purchase-recommendation.html)。

### `get-reservation-utilization`
<a name="cost-explorer_GetReservationUtilization_cli_topic"></a>

以下代码示例演示了如何使用 `get-reservation-utilization`。

**AWS CLI**  
**检索您账户的预留利用率**  
以下 `get-reservation-utilization` 示例检索账户在 2018-03-01 到 2018-08-01 期间所有 t2.nano 实例类型的 RI 利用率。  

```
aws ce get-reservation-utilization \
    --time-period Start=2018-03-01,End=2018-08-01 \
    --filter file://filters.json
```
 的内容`filters.json`：  

```
{
    "Dimensions": {
        "Key": "INSTANCE_TYPE",
        "Values": [
            "t2.nano"
        ]
    }
}
```
输出：  

```
{
    "Total": {
        "TotalAmortizedFee": "0",
        "UtilizationPercentage": "0",
        "PurchasedHours": "0",
        "NetRISavings": "0",
        "TotalActualHours": "0",
        "AmortizedRecurringFee": "0",
        "UnusedHours": "0",
        "TotalPotentialRISavings": "0",
        "OnDemandCostOfRIHoursUsed": "0",
        "AmortizedUpfrontFee": "0"
    },
    "UtilizationsByTime": []
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetReservationUtilization](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-reservation-utilization.html)。

### `get-tags`
<a name="cost-explorer_GetTags_cli_topic"></a>

以下代码示例演示了如何使用 `get-tags`。

**AWS CLI**  
**检索成本分配标签的键和值**  
此示例检索密钥为“Project”且值包含“secretProject”的所有成本分配标签。  
命令:  

```
aws ce get-tags --search-string secretProject --time-period Start=2017-01-01,End=2017-05-18 --tag-key Project
```
输出：  

```
{
  "ReturnSize": 2,
  "Tags": [
    "secretProject1",
    "secretProject2"
  ],
  "TotalSize": 2
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetTags](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-tags.html)。