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-usageseguente recupera l’utilizzo di S3 di un account per il mese di settembre 2017.aws ce get-cost-and-usage \ --time-periodStart=2017-09-01,End=2017-10-01\ --granularityMONTHLY\ --metrics"BlendedCost""UnblendedCost""UsageQuantity"\ --group-byType=DIMENSION,Key=SERVICEType=TAG,Key=Environment\ --filterfile://filters.jsonContenuto 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-stringElastic--time-periodStart=2017-01-01,End=2017-05-18--dimensionSERVICEOutput:
{ "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-periodStart=2017-07-01,End=2017-10-01--group-byType=Dimension,Key=REGION--filterfile://filters.jsonfilters.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 }-
Per informazioni dettagliate sull’API, consulta GetReservationCoverage
in AWS CLI Command Reference.
-
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-recommendationseguente 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-daysSIXTY_DAYS\ --term-in-yearsTHREE_YEARS\ --payment-optionPARTIAL_UPFRONTOutput:
{ "Recommendations": [], "Metadata": { "GenerationTimestamp": "2018-08-08T15:20:57Z", "RecommendationId": "00d59dde-a1ad-473f-8ff2-iexample3330b" } }-
Per informazioni dettagliate sull’API, consulta GetReservationPurchaseRecommendation
inAWS CLI Command Reference.
-
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-utilizationseguente 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-periodStart=2018-03-01,End=2018-08-01\ --filterfile://filters.jsonContenuto 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": [] }-
Per informazioni dettagliate sull’API, consulta GetReservationUtilization
in AWS CLI Command Reference.
-
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-stringsecretProject--time-periodStart=2017-01-01,End=2017-05-18--tag-keyProjectOutput:
{ "ReturnSize": 2, "Tags": [ "secretProject1", "secretProject2" ], "TotalSize": 2 }-
Per informazioni dettagliate sull’API, consulta GetTags
in AWS CLI Command Reference.
-