-1

I found this reference https://docs.aws.amazon.com/sdkforruby/api/Aws/CostExplorer/Client.html#initialize-instance_method for finding cost of ec2 instance usages ,while in implementing phase i got exception.

in gem file i used this gem 'aws-sdk', '~> 3'

   cost_client = Aws::CostExplorer::Client.new(
                 region: "us-east-1",
                 access_key_id: "XXXX",
                 secret_access_key: "XXX"
                )

   resp = cost_client.get_cost_and_usage({
          time_period: { # required
          start: "2019-01-01", # required
          end: "2019-05-01", # required
          },
   granularity: "DAILY", # accepts DAILY, MONTHLY, HOURLY
         filter: {
         or: [
         {
            # recursive Expression
         },
        ],
        and: [
         {
            # recursive Expression
         },
        ],
       not: {
           # recursive Expression
          },
      dimensions: {
         key: "AZ", # accepts AZ, INSTANCE_TYPE, LINKED_ACCOUNT, OPERATION, PURCHASE_TYPE, REGION, SERVICE, USAGE_TYPE, USAGE_TYPE_GROUP, RECORD_TYPE, OPERATING_SYSTEM, TENANCY, SCOPE, PLATFORM, SUBSCRIPTION_ID, LEGAL_ENTITY_NAME, DEPLOYMENT_OPTION, DATABASE_ENGINE, CACHE_ENGINE, INSTANCE_TYPE_FAMILY, BILLING_ENTITY, RESERVATION_ID
        values: ["Value"],
         },
       tags: {
           key: "TagKey",
           values: ["Value"],
         },
        },
       metrics: ["MetricName"],
       group_by: [
         {
           type: "DIMENSION", # accepts DIMENSION, TAG
           key: "GroupDefinitionKey",
         },
       ],
       next_page_token: "NextPageToken",
    })

i got this excetion NameError: uninitialized constant Aws::CostExplorer.how can i get response from this api?

Neeraj Amoli
  • 976
  • 1
  • 9
  • 14

2 Answers2

3

gems you used are not compatible with costexplorer sdk. use this-

  1. gem 'aws-sigv4', '~> 1.1'
  2. gem 'aws-sdk-core', '~> 3.52', '>= 3.52.1'
  3. gem 'aws-sdk-costexplorer', '~> 1.22'

Hope this will help you :)

Neha Jaggi
  • 191
  • 2
  • 7
0

You are using version 3 while your docs point to v2.

Check https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CostExplorer.html, specially the following

This module provides support for AWS Cost Explorer Service. This module is available in the aws-sdk-costexplorer gem.

aromero
  • 25,681
  • 6
  • 57
  • 79