Esempi del servizio Cost Explorer con AWS CLI - AWS Command Line Interface

Esempi del servizio Cost Explorer con AWS CLI

Gli esempi di codice seguenti mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS Command Line Interface con il servizio Cost Explorer.

Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le operazioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati.

Ogni esempio include un link al codice sorgente completo, dove è possibile trovare le istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Operazioni

L’esempio di codice seguente mostra come utilizzare get-cost-and-usage.

AWS CLI

Come recuperare l’utilizzo S3 di un account per il mese di settembre 2017

L’esempio get-cost-and-usage seguente recupera l’utilizzo di S3 di un account per il mese di settembre 2017.

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

Contenuto di filters.json.

{ "Dimensions": { "Key": "SERVICE", "Values": [ "Amazon Simple Storage Service" ] } }

Output:

{ "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" } } } ] } ] }
  • Per informazioni dettagliate sull’API, consulta GetCostAndUsage in AWS CLI Command Reference.

L’esempio di codice seguente mostra come utilizzare get-dimension-values.

AWS CLI

Come recuperare i tag della dimensione SERVICE, con il valore “Elastic”

Questo esempio recupera i tag per la dimensione SERVICE, con il valore “Elastic” dal 1° gennaio 2017 al 18 maggio 2017.

Comando:

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

Output:

{ "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 }
  • Per informazioni dettagliate sull’API, consulta GetDimensionValues in AWS CLI Command Reference.

L’esempio di codice seguente mostra come utilizzare get-reservation-coverage.

AWS CLI

Come recuperare la copertura delle prenotazioni per le istanze EC2 t2.nano nella Regione us-east-1

Questo esempio recupera la copertura delle prenotazioni per le istanze EC2 t2.nano nella Regione us-east-1 per luglio-settembre 2017.

Comando:

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" ] } } ] }

Output:

{ "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 }

L’esempio di codice seguente mostra come utilizzare get-reservation-purchase-recommendation.

AWS CLI

Come recuperare le raccomandazioni di prenotazione per le istanze EC2 riservate con pagamento anticipato parziale con una durata di tre anni

L’esempio get-reservation-purchase-recommendation seguente recupera le raccomandazioni di prenotazione per le istanze EC2 riservate con pagamento anticipato parziale con una durata di tre anni, in base agli ultimi 60 giorni di utilizzo di 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

Output:

{ "Recommendations": [], "Metadata": { "GenerationTimestamp": "2018-08-08T15:20:57Z", "RecommendationId": "00d59dde-a1ad-473f-8ff2-iexample3330b" } }

L’esempio di codice seguente mostra come utilizzare get-reservation-utilization.

AWS CLI

Come concedere l’autorizzazione a recuperare l’utilizzo della prenotazione per l’account

L’esempio get-reservation-utilization seguente recupera l’utilizzo delle istanze riservate per tutti i tipi di istanza t2.nano dal 01-03-2018 al 01-08-2018 per l’account.

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

Contenuto di filters.json.

{ "Dimensions": { "Key": "INSTANCE_TYPE", "Values": [ "t2.nano" ] } }

Output:

{ "Total": { "TotalAmortizedFee": "0", "UtilizationPercentage": "0", "PurchasedHours": "0", "NetRISavings": "0", "TotalActualHours": "0", "AmortizedRecurringFee": "0", "UnusedHours": "0", "TotalPotentialRISavings": "0", "OnDemandCostOfRIHoursUsed": "0", "AmortizedUpfrontFee": "0" }, "UtilizationsByTime": [] }

L’esempio di codice seguente mostra come utilizzare get-tags.

AWS CLI

Come recuperare chiavi e valori per un tag di allocazione dei costi

Questo esempio recupera tutti i tag di allocazione dei costi con la chiave “Project” e un valore che contiene “secretProject”.

Comando:

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

Output:

{ "ReturnSize": 2, "Tags": [ "secretProject1", "secretProject2" ], "TotalSize": 2 }
  • Per informazioni dettagliate sull’API, consulta GetTags in AWS CLI Command Reference.