

# 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월 01일부터 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월\$19월 동안 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
```
filter.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**  
**비용 할당 태그의 키와 값을 검색하는 방법**  
이 예제에서는 키가 "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)를 참조하세요.