是否有适用于Amazon Web Services PRICING的API?

时间:2010-09-03 14:10:51

标签: api amazon-web-services amazon-s3 amazon-ec2

是否有任何API在Amazon Web Services上具有最新定价?可以查询的东西,例如,给定区域的最新价格S3,或EC2等。

谢谢

12 个答案:

答案 0 :(得分:38)

<强>更新

AWS现在有定价API:https://aws.amazon.com/blogs/aws/new-aws-price-list-api/

原始回答:

这是我之前要求的(通过AWS传播者和调查),但尚未提及。我想AWS的人们有更多有趣的创新。

正如@brokenbeatnik指出的那样,有一个现货价格历史的API。 API文档:http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSpotPriceHistory.html

我觉得奇怪的是,现货价格历史记录中有一个官方 API,但他们并没有同时为按需服务执行此操作。无论如何,要回答这个问题,是的,您可以查询宣传的 AWS定价 ......

我能想到的最好的方法是检查各种服务定价页面的(客户端)来源。在那里你会发现这些表是用JS构建的,并且填充了JSON数据,你可以自己获取数据。 E.g:

这只是战斗的一半,接下来你必须选择对象格式以获得你想要的值,例如,在Python中,它获得了弗吉尼亚州的Hi-CPU按需超大Linux实例价格: / p>

>>> import json
>>> import urllib2
>>> response = urllib2.urlopen('http://aws.amazon.com/ec2/pricing/pricing-on-demand-instances.json')
>>> pricejson = response.read()
>>> pricing = json.loads(pricejson)
>>> pricing['config']['regions'][0]['instanceTypes'][3]['sizes'][1]['valueColumns'][0]['prices']['USD']
u'0.68'

免责声明:显然,这不是AWS认可的API,因此我不建议期望数据格式的稳定性或甚至源的持续存在。但它就在那里,并且它将定价数据转录为静态配置/源文件!

答案 1 :(得分:14)

对于想要使用amazon api中使用“t1.micro”之类的数据的人来说,这里是一个翻译数组

type_translation = {
    'm1.small' : ['stdODI', 'sm'],
    'm1.medium' : ['stdODI', 'med'],
    'm1.large' : ['stdODI', 'lg'],
    'm1.xlarge' : ['stdODI', 'xl'],
    't1.micro' : ['uODI', 'u'],
    'm2.xlarge' : ['hiMemODI', 'xl'],
    'm2.2xlarge' : ['hiMemODI', 'xxl'],
    'm2.4xlarge' : ['hiMemODI', 'xxxxl'],
    'c1.medium' : ['hiCPUODI', 'med'],
    'c1.xlarge' : ['hiCPUODI', 'xl'],
    'cc1.4xlarge' : ['clusterComputeI', 'xxxxl'],
    'cc2.8xlarge' : ['clusterComputeI', 'xxxxxxxxl'],
    'cg1.4xlarge' : ['clusterGPUI', 'xxxxl'],
    'hi1.4xlarge' : ['hiIoODI', 'xxxx1']
}
region_translation = {
    'us-east-1' : 'us-east',
    'us-west-2' : 'us-west-2',
    'us-west-1' : 'us-west',
    'eu-west-1' : 'eu-ireland',
    'ap-southeast-1' : 'apac-sin',
    'ap-northeast-1' : 'apac-tokyo',
    'sa-east-1' : 'sa-east-1'
}

答案 2 :(得分:8)

我创造了一个快速的&amp; Python中的脏API,用于访问这些JSON文件中的定价数据并将其转换为相关值(正确的翻译和正确的实例类型)。

您可以在此处获取代码:https://github.com/erans/ec2instancespricing

在此处阅读更多相关内容:http://forecastcloudy.net/2012/04/03/quick-dirty-api-for-accessing-amazon-web-services-aws-ec2-pricing-data/

您可以将此文件用作模块并调用函数以获取包含结果的Python字典,或者您可以将其用作命令行工具以获得输出是人类可读的表,JSON或CSV以用于与其他命令行工具结合使用。

答案 3 :(得分:4)

通过下面的链接可以找到一个很好的API,您可以查询AWS定价。

http://info.awsstream.com

如果您使用过滤器稍微玩一下,可以看到如何构建查询以返回您之后的特定信息,例如:区域,实例类型等。例如,要返回包含eu-west-1区域linux实例的EC2定价的json,您可以按照以下格式设置查询格式。

http://info.awsstream.com/instances.json?region=eu-west-1&os=linux

在上面的查询中用jml替换json,以xml格式返回信息。

注意 - 与上面其他贡献者发布的网址类似,我不相信这是官方批准的AWS API。但是,根据我在过去几天进行的一些抽查,我可以确认在发布时价格信息似乎是正确的。

答案 4 :(得分:1)

我不相信有一个API可以涵盖标准服务的一般当前价格。但是,对于EC2,您可以查看现货价格历史记录,这样您就不必猜测现货实例的市场价格。有关详细信息,请访问:

http://docs.amazonwebservices.com/AWSEC2/latest/DeveloperGuide/using-spot-instances-history.html

答案 5 :(得分:1)

我也需要一个API来检索AWS定价。鉴于可用于AWS资源的大量API,我很惊讶地发现没什么特别的。

我的首选语言是Ruby,因此我编写了一个名为AWSCosts的Gem,它提供对AWS定价的编程访问。

以下是如何查找m1.medium Linux实例的按需价格的示例。

  

AWSCosts.region( '我们 - 东 - 1')ec2.on_demand。(:LINUX)。价格( 'm1.medium')

答案 6 :(得分:1)

对于那些需要全面的AWS实例定价数据(EC2,RDS,ElastiCache和Redshift)的人来说,下面是Eran Sandler建议的Python模块:

https://github.com/ilia-semenov/awspricingfull

它包含上一代实例以及当前代实例(包括最新的d2系列),保留和按需定价。提供JSON,表格和CSV格式。

答案 7 :(得分:0)

如果有人需要Rails等,我在Yaml中制作了Gist正向和反向名称。

答案 8 :(得分:0)

另一个快速&amp;脏,但转换为更方便的最终数据格式

 class CostsAmazon(object):
    '''Class for general info on the Amazon EC2 compute cloud.
    '''
    def __init__(self):
        '''Fetch a bunch of instance cost data from Amazon and convert it
        into the following form (as self.table):

        table['us-east']['linux']['m1']['small']['light']['ondemand']['USD']
        '''
        #
        #    tables_raw['ondemand']['config']['regions'
        #        ][0]['instanceTypes'][0]['sizes'][0]['valueColumns'][0
        #        ]['prices']['USD']
        #
        # structure of tables_raw:
        # ┃
        # ┗━━[key]
        #    ┣━━['use']        # an input 3 x ∈ { 'light', 'medium', ... }
        #    ┣━━['os']         # an input 2 x ∈ { 'linux', 'mswin' }
        #    ┣━━['scheduling'] # an input
        #    ┣━━['uri']        # an input (see dict above)
        #    ┃                 # the core output from Amazon follows
        #    ┣━━['vers'] == 0.01
        #    ┗━━['config']:
        #   *   ┣━━['regions']: 7 x
        #       ┃  ┣━━['region'] == ∈ { 'us-east', ... }
        #   *   ┃  ┗━━['instanceTypes']: 7 x
        #       ┃     ┣━━['type']: 'stdODI'
        #   *   ┃     ┗━━['sizes']: 4 x
        #       ┃        ┗━━['valueColumns']
        #       ┃           ┣━━['size']: 'sm'
        #   *   ┃           ┗━━['valueColumns']: 2 x
        #       ┃              ┣━━['name']: ~ 'linux'
        #       ┃              ┗━━['prices']
        #       ┃                 ┗━━['USD']: ~ '0.080'
        #       ┣━━['rate']: ~ 'perhr'
        #       ┣━━['currencies']: ∈ { 'USD', ... }
        #       ┗━━['valueColumns']: [ 'linux', 'mswin' ]
        #
        # The valueColumns thing is weird, it looks like they're trying
        #   to constrain actual data to leaf nodes only, which is a little
        #   bit of a conceit since they have lists in several levels.  So
        #   we can obtain the *much* more readable:
        #
        #     tables['regions']['us-east']['m1']['linux']['ondemand'
        #         ]['small']['light']['USD']
        #
        # structure of the reworked tables:
        # ┃
        # ┗━━[<region>]: 7 x ∈ { 'us-east', ... }
        #    ┗━━[<os>]: 2 x ∈ { 'linux', 'mswin' }  # oses
        #       ┗━━[<type>]: 7 x ∈ { 'm1', ... }
        #          ┗━━[<scheduling>]: 2 x ∈ { 'ondemand', 'reserved' }
        #             ┗━━[<size>]: 4 x ∈ { 'small', ... }
        #                ┗━━[<use>]: 3 x ∈ { 'light', 'medium', ... }
        #                   ┗━━[<currency>]: ∈ { 'USD', ... }
        #                      ┗━━> ~ '0.080' or None
        uri_base = 'http://aws.amazon.com/ec2/pricing'
        tables_raw = {
            'ondemand': {'scheduling': 'ondemand',
                         'uri': '/pricing-on-demand-instances.json',
                         'os': 'linux', 'use': 'light'},
            'reserved-light-linux':  {
                'scheduling': 'ondemand',
                'uri': 'ri-light-linux.json', 'os': 'linux', 'use': 'light'},
            'reserved-light-mswin': {
                'scheduling': 'ondemand',
                'uri': 'ri-light-mswin.json', 'os': 'mswin', 'use': 'light'},
            'reserved-medium-linux': {
                'scheduling': 'ondemand',
                'uri': 'ri-medium-linux.json', 'os': 'linux', 'use': 'medium'},
            'reserved-medium-mswin': {
                'scheduling': 'ondemand',
                'uri': 'ri-medium-mswin.json', 'os': 'mswin', 'use': 'medium'},
            'reserved-heavy-linux': {
                'scheduling': 'ondemand',
                'uri': 'ri-heavy-linux.json', 'os': 'linux', 'use': 'heavy'},
            'reserved-heavy-mswin': {
                'scheduling': 'ondemand',
                'uri': 'ri-heavy-mswin.json', 'os': 'mswin', 'use': 'heavy'},
            }
        for key in tables_raw:
            # expand to full URIs
            tables_raw[key]['uri'] = (
                '%s/%s'% (uri_base, tables_raw[key]['uri']))
            # fetch the data from Amazon
            link = urllib2.urlopen(tables_raw[key]['uri'])
            # adds keys: 'vers' 'config'
            tables_raw[key].update(json.loads(link.read()))
            link.close()
            # canonicalize the types - the default is pretty annoying.
            #
        self.currencies = set()
        self.regions = set()
        self.types = set()
        self.intervals = set()
        self.oses = set()
        self.sizes = set()
        self.schedulings = set()
        self.uses = set()

        self.footnotes = {}
        self.typesizes = {}   # self.typesizes['m1.small'] = [<region>...]
        self.table = {}

        # grovel through Amazon's tables_raw and convert to something orderly:
        for key in tables_raw:
            scheduling = tables_raw[key]['scheduling']
            self.schedulings.update([scheduling])
            use = tables_raw[key]['use']
            self.uses.update([use])
            os =  tables_raw[key]['os']
            self.oses.update([os])
            config_data = tables_raw[key]['config']
            self.currencies.update(config_data['currencies'])
            for region_data in config_data['regions']:
                region = self.instance_region_from_raw(region_data['region'])
                self.regions.update([region])
                if 'footnotes' in region_data:
                    self.footnotes[region] = region_data['footnotes']
                for instance_type_data in region_data['instanceTypes']:
                    instance_type = self.instance_types_from_raw(
                        instance_type_data['type'])
                    self.types.update([instance_type])
                    for size_data in instance_type_data['sizes']:
                        size = self.instance_size_from_raw(size_data['size'])
                        typesize = '%s.%s' % (instance_type, size)
                        if typesize not in self.typesizes:
                            self.typesizes[typesize] = set()
                        self.typesizes[typesize].update([region])
                        self.sizes.update([size])
                        for size_values in size_data['valueColumns']:
                            interval = size_values['name']
                            self.intervals.update([interval])
                            for currency in size_values['prices']:
                                cost = size_values['prices'][currency]
                                self.table_add_row(region, os, instance_type,
                                                   size, use, scheduling,
                                                   currency, cost)

    def table_add_row(self, region, os, instance_type, size, use, scheduling,
                      currency, cost):
        if cost == 'N/A*':
            return
        table = self.table
        for key in [region, os, instance_type, size, use, scheduling]:
            if key not in table:
                table[key] = {}
            table = table[key]
        table[currency] = cost

    def instance_region_from_raw(self, raw_region):
        '''Return a less intelligent given EC2 pricing name to the
        corresponding region name.
        '''
        regions = {
            'apac-tokyo' : 'ap-northeast-1',
            'apac-sin'   : 'ap-southeast-1',
            'eu-ireland' : 'eu-west-1',
            'sa-east-1'  : 'sa-east-1',
            'us-east'    : 'us-east-1',
            'us-west'    : 'us-west-1',
            'us-west-2'  : 'us-west-2',
            }
        return regions[raw_region] if raw_region in regions else raw_region

    def instance_types_from_raw(self, raw_type):
        types = {
            # ondemand                 reserved
            'stdODI'          : 'm1',  'stdResI'         : 'm1',
            'uODI'            : 't1',  'uResI'           : 't1',
            'hiMemODI'        : 'm2',  'hiMemResI'       : 'm2',
            'hiCPUODI'        : 'c1',  'hiCPUResI'       : 'c1',
            'clusterComputeI' : 'cc1', 'clusterCompResI' : 'cc1',
            'clusterGPUI'     : 'cc2', 'clusterGPUResI'  : 'cc2',
            'hiIoODI'         : 'hi1', 'hiIoResI'        : 'hi1'
            }
        return types[raw_type]

    def instance_size_from_raw(self, raw_size):
        sizes = {
            'u'         : 'micro',
            'sm'        : 'small',
            'med'       : 'medium',
            'lg'        : 'large',
            'xl'        : 'xlarge',
            'xxl'       : '2xlarge',
            'xxxxl'     : '4xlarge',
            'xxxxxxxxl' : '8xlarge'
            }
        return sizes[raw_size]

    def cost(self, region, os, instance_type, size, use, scheduling,
             currency):
        try:
            return self.table[region][os][instance_type][
                size][use][scheduling][currency]
        except KeyError as ex:
            return None

答案 9 :(得分:0)

这是另一个未经批准的“api”,它涵盖了保留的实例:http://aws.amazon.com/ec2/pricing/pricing-reserved-instances.json

答案 10 :(得分:0)

没有定价api,但上面提到了非常好的价格。 除了ec2价格开膛手,我想分享我的rds和弹性价格开膛手:

https://github.com/evgeny-gridasov/rdsinstancespricing https://github.com/evgeny-gridasov/elasticachepricing

答案 11 :(得分:0)

reply to a similar question列出了包含价格的所有.js个文件,这些文件几乎不是JSON文件(只删除了callback(...);个语句。)

以下是Linux On Demand价格的例子:http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-od.js

(获取完整列表directly on that reply