Advertisement
JaimitoM

Untitled

May 21st, 2022
979
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.List;
  7. import java.util.stream.Collectors;
  8.  
  9. public class CostExplorerTest {
  10.     public static void main(String[] args) {
  11.         StaticCredentialsProvider credentials = StaticCredentialsProvider.create(
  12.                 AwsBasicCredentials.create( Environment.ACCESS_KEY, Environment.SECRET_ACCESS_KEY )
  13.         );
  14.  
  15.         CostExplorerClient costClient = CostExplorerClient.builder()
  16.                 .credentialsProvider( credentials )
  17.                 .build();
  18.  
  19.         DateInterval period = DateInterval.builder()
  20.                 .start( "2022-05-01" )
  21.                 .end( "2022-05-20" )
  22.                 .build();
  23.  
  24.         GetCostAndUsageResponse result = costClient.getCostAndUsage( GetCostAndUsageRequest.builder()
  25.                 .granularity( Granularity.DAILY )
  26.                 .timePeriod( period )
  27.                 .metrics( "UnblendedCost" )
  28.                 .groupBy( GroupDefinition.builder().type( GroupDefinitionType.DIMENSION )
  29.                         .key( "SERVICE" )
  30.                         .build() )
  31.                 .build()
  32.         );
  33.  
  34.  
  35.         // Mostrar resultados en consola
  36.         for (ResultByTime r : result.resultsByTime()) {
  37.             List<Group> groups = r.groups();
  38.             System.out.println(r.timePeriod());
  39.             groups.forEach( group -> {
  40.                 String service = group.keys().stream().collect( Collectors.joining() );
  41.                 MetricValue metric = group.metrics().get( "UnblendedCost" );
  42.                 System.out.println( service + " - " + metric.amount() + " " + metric.unit() );
  43.             } );
  44.             System.out.println("----------------------------------");
  45.         }
  46.     }
  47. }
  48.  
Advertisement
RAW Paste Data Copied
Advertisement