Advertisement
JaimitoM

Untitled

May 21st, 2022
961
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
  2. import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
  3. import software.amazon.awssdk.services.costexplorer.CostExplorerClient;
  4. import software.amazon.awssdk.services.costexplorer.model.*;
  5.  
  6. import java.util.stream.Collectors;
  7.  
  8. public class CostExplorerTest {
  9.     public static void main(String[] args) {
  10.         var credentials = StaticCredentialsProvider.create(
  11.                 AwsBasicCredentials.create( Environment.ACCESS_KEY, Environment.SECRET_ACCESS_KEY )
  12.         );
  13.  
  14.         CostExplorerClient costClient = CostExplorerClient.builder()
  15.                 .credentialsProvider( credentials )
  16.                 .build();
  17.  
  18.         DateInterval period = DateInterval.builder()
  19.                 .start( "2022-05-01" )
  20.                 .end( "2022-05-20" )
  21.                 .build();
  22.  
  23.         GetCostAndUsageResponse result = costClient.getCostAndUsage( GetCostAndUsageRequest.builder()
  24.                 .granularity( Granularity.DAILY )
  25.                 .timePeriod( period )
  26.                 .metrics( "UnblendedCost" )
  27.                 .groupBy( GroupDefinition.builder().type( GroupDefinitionType.DIMENSION )
  28.                         .key( "SERVICE" )
  29.                         .build() )
  30.                 .build()
  31.         );
  32.  
  33.  
  34.         // Mostrar resultados en consola
  35.         for (var r : result.resultsByTime()) {
  36.             var groups = r.groups();
  37.             System.out.println(r.timePeriod());
  38.             groups.forEach( group -> {
  39.                 var service = group.keys().stream().collect( Collectors.joining() );
  40.                 var metric = group.metrics().get( "UnblendedCost" );
  41.                 System.out.println( service + " - " + metric.amount() + " " + metric.unit() );
  42.             } );
  43.             System.out.println("----------------------------------");
  44.         }
  45.     }
  46. }
  47.  
Advertisement
RAW Paste Data Copied
Advertisement