-1

我发现此参考https://docs.aws.amazon.com/sdkforruby/api/Aws/CostExplorer/Client.html#initialize-instance_method 用于查找 ec2 实例使用的成本,而在实施阶段我遇到了异常。

在 gem 文件中我使用了这个 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",
    })

我得到了这个异常 NameError: uninitialized constant Aws::CostExplorer.我怎样才能从这个 api 得到响应?

4

2 回答 2

3

您使用的 gem 与costexplorer sdk不兼容。用这个-

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

希望对你有帮助 :)

于 2019-05-16T06:07:27.937 回答
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.

于 2019-05-15T17:18:17.260 回答