使用 的 Cost Explorer Service 範例 AWS CLI - AWS Command Line Interface

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

使用 的 Cost Explorer Service 範例 AWS CLI

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

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

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

主題

動作

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 GetCostAndUsage中的 。 AWS CLI

下列程式碼範例示範如何使用 get-dimension-values

AWS CLI

若要擷取維度 的標籤SERVICE,值為「彈性」

此範例會擷取維度 的標籤SERVICE,其值為「彈性」,從 2017 年 1 月 1 日至 2017 年 5 月 18 日。

命令:

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 }

下列程式碼範例示範如何使用 get-reservation-coverage

AWS CLI

擷取 us-east-1 區域中 EC2 t2.nano 執行個體的保留涵蓋範圍

此範例會擷取 2017 年 EC2 7 月至 9 月 us-east-1 區域中 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 }

下列程式碼範例示範如何使用 get-reservation-purchase-recommendation

AWS CLI

擷取EC2RIs三年期間的部分預付保留建議

下列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" } }

下列程式碼範例示範如何使用 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": [] }

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 GetTags中的 。 AWS CLI