以编程方式获得ec2定价?

mor*_*fys 41 amazon-ec2 amazon-web-services

有没有办法以编程方式获得AWS定价(每种实例类型的每小时成本,每GB的成本/ S3上的存储月份等)?

还有成本监测工具吗?例如,是否有一种工具可以按小时报告您的EC2实例使用情况(与每月一样,这是亚马逊的做法)?

提前致谢.

okr*_*asz 59

更新:

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

原始答案:

价格表以JSONP文件的形式提供(您需要剥离函数调用),这些文件由AWS定价页面使用.每个表(以及表的每个选项卡)都有单独的JSON文件.它可能不是API,但绝对是计算机可消化的.以下是支持EC2定价页面的列表(截至2014年12月17日):

警告:端点会不时更改,并且通常旧的URL仍然存在旧值.最好检查当前状态,而不是依赖此线程中提供的链接.

因此,这是一个简短的命令,用于从任何AWS定价页面获取当前集或URL.基于EC2的示例.在Linux或Cygwin上运行它.实际上这个命令用于创建上面的列表.

curl http://aws.amazon.com/ec2/pricing/ 2>/dev/null | grep 'model:' | sed -e "s/.*'\(.*\)'.*/http:\\1/"
Run Code Online (Sandbox Code Playgroud)

对于那些不喜欢命令行的人,你也可以登录一个网页浏览器网络控制台(用F12到达那里),用JS对象过滤:

在此输入图像描述

  • 只是更新,“curl -L http://aws.amazon.com/ec2/pricing/”不再带来预期结果。 (2认同)

art*_*hoo 37

只是为了让您知道他们似乎已经更改了JSON地址.它包括新的C3实例类型

2014年1月21日更新:地址再次更改.请注意,这些是带有回调函数的JS文件,应该将其删除,以使其成为可解析的JSON.

2014年9月21日更新:地址再次更改并包含新的T2实例类型.要被视为JSON文件,应删除初始注释和回调函数,并将键用双引号括起来.

一经请求

保留灯

保留介质

保留重

其他

以前的终点:http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-od.js


jus*_*guy 8

使用 aws cli (在下面的示例中,我还介绍了如何使用 jq 执行相同的操作)

获取服务代码列表:

aws pricing describe-services --region us-east-1
Run Code Online (Sandbox Code Playgroud)

获取服务代码列表(使用 jq):

aws pricing describe-services --region us-east-1 | jq -r '.Services[] | .ServiceCode'
Run Code Online (Sandbox Code Playgroud)

它将返回如下值

AmazonEC2
AmazonS3
AmazonRoute53
[...]



获取给定服务代码的属性列表:

aws pricing describe-services --service-code AmazonEC2 --region us-east-1
Run Code Online (Sandbox Code Playgroud)

获取给定服务代码的属性列表(使用 jq):

aws pricing describe-services --service-code AmazonEC2 --region us-east-1 | jq -r '.Services[] | .AttributeNames[]'
Run Code Online (Sandbox Code Playgroud)

它将返回如下值

instancesku
位置
内存
vcpu
卷类型
[...]



既然您已经有了服务代码和属性,就可以获取定价信息:

(这将需要一段时间,因为它是服务代码的每个 sku,所以我将进一步展示使用过滤的示例)

aws pricing get-products --service-code AmazonEC2 --region us-east-1
Run Code Online (Sandbox Code Playgroud)

instanceType现在您已经有了使用过滤器 on和另一个 for 的服务代码和属性来获取定价信息location

aws pricing get-products --service-code AmazonEC2 --filters "Type=TERM_MATCH,Field=instanceType,Value=m5.xlarge" "Type=TERM_MATCH,Field=location,Value=US East (N. Virginia)" --region us-east-1
Run Code Online (Sandbox Code Playgroud)

现在您已经有了使用过滤器 oninstanceType和另一个 for location(使用 jq)的服务代码和属性来获取定价信息:

aws pricing get-products --service-code AmazonEC2 --filters "Type=TERM_MATCH,Field=instanceType,Value=m5.xlarge" "Type=TERM_MATCH,Field=location,Value=US East (N. Virginia)" --region us-east-1 | jq -rc '.PriceList[]' | jq -r '[ .product.attributes.servicecode, .product.attributes.location, .product.attributes.instancesku?, .product.attributes.instanceType, .product.attributes.usagetype, .product.attributes.operatingSystem, .product.attributes.memory, .product.attributes.physicalProcessor, .product.attributes.processorArchitecture, .product.attributes.vcpu, .product.attributes.currentGeneration, .terms.OnDemand[].priceDimensions[].unit, .terms.OnDemand[].priceDimensions[].pricePerUnit.USD, .terms.OnDemand[].priceDimensions[].description] | @csv'
Run Code Online (Sandbox Code Playgroud)

它将返回如下值

"AmazonEC2","US East (N. Virginia)","EWZRARGKPMTYQJFP","m5.xlarge","UnusedDed:m5.xlarge","Linux","16 GiB","Intel Xeon Platinum 8175 (Skylake)","64-bit","4","Yes","Hrs","0.6840000000","$0.684 per Dedicated Unused Reservation Linux with SQL Std m5.xlarge Instance Hour"
[...]
Run Code Online (Sandbox Code Playgroud)


Ale*_*ane 6

除了@arturhoo 的回答提供了 EC2 位置

您可以使用CLI工具获取历史价格

aws ec2 describe-spot-price-history \
    --instance-types m1.xlarge \
    --product-description "Linux/UNIX (Amazon VPC)" \
    --start-time 2016-10-31T03:00:00 \
    --end-time 2016-10-31T03:16:00 \
    --query 'SpotPriceHistory[*].[Timestamp,SpotPrice]'
Run Code Online (Sandbox Code Playgroud)

采用2016 年 10 月 31 日星期一(UTC)3:00am之间的现货价格3:16am

[
    [
        "2016-10-31T03:06:12.000Z", 
        "0.041500"
    ], 
    [
        "2016-10-31T03:00:26.000Z", 
        "0.041600"
    ], 
    [
        "2016-10-31T02:59:14.000Z", 
        "0.041500"
    ], 
    [
        "2016-10-31T02:00:18.000Z", 
        "0.040600"
    ], 
    [
        "2016-10-30T23:55:06.000Z", 
        "0.043200"
    ]
]
Run Code Online (Sandbox Code Playgroud)


小智 6

除了官方 AWS JSON 端点之外,https://ec2.shop也是一个选项。它响应 json 和基于文本的(因此您可以使用 grep、awk 等)。

curl -L 'ec2.shop?filter=.large'
Instance Type    Memory             vCPUs  Storage               Network             Price       Monthly     Spot Price
m6g.large        8 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0770      56.210      0.0357
t3.large         8 GiB            2 vCPUs  EBS only              Up to 5 Gigabit     0.0832      60.736      0.0250
m5a.large        8 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0860      62.780      0.0345
i3en.large       16 GiB           2 vCPUs  1 x 1250 NVMe SSD     Up to 25 Gigabit    0.2260      164.980     0.0678
r4.large         15.25 GiB        2 vCPUs  EBS only              Up to 10 Gigabit    0.1330      97.090      0.0343
r5.large         16 GiB           2 vCPUs  EBS only              Up to 10 Gigabit    0.1260      91.980      0.0356
r5a.large        16 GiB           2 vCPUs  EBS only              10 Gigabit          0.1130      82.490      0.0356
r5dn.large       16 GiB           2 vCPUs  1 x 75 NVMe SSD       Up to 25 Gigabit    0.1670      121.910     0.0356
t3a.large        8 GiB            2 vCPUs  EBS only              Up to 5 Gigabit     0.0752      54.896      0.0226
m4.large         8 GiB            2 vCPUs  EBS only              Moderate            0.1000      73.000      0.0362
i3.large         15.25 GiB        2 vCPUs  1 x 475 NVMe SSD      Up to 10 Gigabit    0.1560      113.880     0.0468
m5dn.large       8 GiB            2 vCPUs  1 x 75 NVMe SSD       Up to 25 Gigabit    0.1360      99.280      0.0340
m5d.large        8 GiB            2 vCPUs  1 x 75 NVMe SSD       Up to 10 Gigabit    0.1130      82.490      0.0340
t2.large         8 GiB            2 vCPUs  EBS only              Low to Moderate     0.0928      67.744      0.0278
z1d.large        16 GiB           2 vCPUs  1 x 75 NVMe SSD       Up to 10 Gigabit    0.1860      135.780     0.0558
c5.large         4 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0850      62.050      0.0324
m5ad.large       8 GiB            2 vCPUs  1 x 75 NVMe SSD       Up to 10 Gigabit    0.1030      75.190      0.0345
r6g.large        16 GiB           2 vCPUs  EBS only              Up to 10 Gigabit    0.1008      73.584      0.0374
r5d.large        16 GiB           2 vCPUs  1 x 75 NVMe SSD       10 Gigabit          0.1440      105.120     0.0356
r5n.large        16 GiB           2 vCPUs  EBS only              Up to 25 Gigabit    0.1490      108.770     0.0356
r5ad.large       16 GiB           2 vCPUs  1 x 75 NVMe SSD       10 Gigabit          0.1310      95.630      0.0356
c6g.large        4 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0680      49.640      0.0340
m5n.large        8 GiB            2 vCPUs  EBS only              Up to 25 Gigabit    0.1190      86.870      0.0340
c4.large         3.75 GiB         2 vCPUs  EBS only              Moderate            0.1000      73.000      0.0308
c5a.large        4 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0770      56.210      0.0324
c5d.large        4 GiB            2 vCPUs  1 x 50 NVMe SSD       Up to 10 Gigabit    0.0960      70.080      0.0324
m5.large         8 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0960      70.080      0.0341
c5n.large        5.25 GiB         2 vCPUs  EBS only              Up to 25 Gigabit    0.1080      78.840      0.0326
a1.large         4 GiB            2 vCPUs  EBS only              Up to 10 Gigabit    0.0510      37.230      0.0227
c3.large         3.75 GiB         2 vCPUs  2 x 16 SSD            Moderate            0.1050      76.650      0.0294
r3.large         15.25 GiB        2 vCPUs  1 x 32 SSD            Moderate            0.1660      121.180     0.0323
m1.large         7.5 GiB          2 vCPUs  2 x 420 SSD           Moderate            0.1750      127.750     0.0175
m3.large         7.5 GiB          2 vCPUs  1 x 32 SSD            Moderate            0.1330      97.090      0.0308
Run Code Online (Sandbox Code Playgroud)

还有这样的 JSON:

curl -sL 'ec2.shop?filter=m4' -H 'accept: json' | jq .
{
  "Prices": [
    {
      "InstanceType": "m4.16xlarge",
      "Memory": "256 GiB",
      "VCPUS": 64,
      "Storage": "EBS only",
      "Network": "20 Gigabit",
      "Cost": 3.2,
      "MonthlyPrice": 2336,
      "SpotPrice": "0.9479"
    },
    {
      "InstanceType": "m4.large",
      "Memory": "8 GiB",
      "VCPUS": 2,
      "Storage": "EBS only",
      "Network": "Moderate",
      "Cost": 0.1,
      "MonthlyPrice": 73,
      "SpotPrice": "0.0362"
    },
    {
      "InstanceType": "m4.2xlarge",
      "Memory": "32 GiB",
      "VCPUS": 8,
      "Storage": "EBS only",
      "Network": "High",
      "Cost": 0.4,
      "MonthlyPrice": 292,
      "SpotPrice": "0.1504"
    },
    {
      "InstanceType": "m4.4xlarge",
      "Memory": "64 GiB",
      "VCPUS": 16,
      "Storage": "EBS only",
      "Network": "High",
      "Cost": 0.8,
      "MonthlyPrice": 584,
      "SpotPrice": "0.3168"
    },
    {
      "InstanceType": "m4.xlarge",
      "Memory": "16 GiB",
      "VCPUS": 4,
      "Storage": "EBS only",
      "Network": "High",
      "Cost": 0.2,
      "MonthlyPrice": 146,
      "SpotPrice": "0.0661"
    },
    {
      "InstanceType": "m4.10xlarge",
      "Memory": "160 GiB",
      "VCPUS": 40,
      "Storage": "EBS only",
      "Network": "10 Gigabit",
      "Cost": 2,
      "MonthlyPrice": 1460,
      "SpotPrice": "0.7050"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)