

# AWS CLI を使用した Cost Explorer Service の例
<a name="cli_cost-explorer_code_examples"></a>

次のコード例は、Cost Explorer Service で AWS Command Line Interfaceを使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**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**  
**3 年契約の一部前払い EC2 RI の予約の推奨事項を取得するには**  
次の `get-reservation-purchase-recommendation` の例では、過去 60 日間の EC2 の使用状況に基づいて、3 年契約の一部前払い 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**  
**コスト配分タグのキーと値を取得するには**  
この例では、「プロジェクト」のキーと「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)」を参照してください。