get ec2 pricing programmatically?

Amazon Ec2Amazon Web-Services

Amazon Ec2 Problem Overview


Is there a way to get AWS pricing programmatically (cost per hour of each instance type, cost per GB/month of storage on S3, and etc)?

Also, are there cost monitoring tools? For example, is there a tool that can report your EC2 instance usage on an hourly basis (versus a monthly basis, which is what Amazon does)?

Thanks in advance.

Amazon Ec2 Solutions


Solution 1 - Amazon Ec2

UPDATE:

There is now AWS pricing API: https://aws.amazon.com/blogs/aws/new-aws-price-list-api/

Orginal answer:

The price lists are available in form of JSONP files (you need to strip off function call) which are used by the AWS pricing pages. Each table (and each tab for table) has separate JSON file. It is not an API maybe, but definitely computer digestible. Here is a list that supports EC2 pricing page (as of 17 December 2014):

WARNING: The endpoints change from time to time and often old URL is still there with old values. It is best to check what is the current status rather than relying on links provided in this thread.

So, here is a short command to get current set or URLs from any AWS pricing page. Example based on EC2. Run it on Linux or Cygwin. Actually this command was used to create the list above.

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

For those who don't like command line, you can also check in a web browser network console (you get there with F12), filter with JS objects:

enter image description here

Solution 2 - Amazon Ec2

Just to let you know that they seem to have changed the JSON addresses. It includes the new C3 instance types

Update 01/21/2014: addresses changed again. Please note that these are JS files with a callback function that should be removed so that it becomes a parsable JSON.

Update 09/21/2014: addresses changed once again and include the new T2 instace types. To be treated as JSON files, the initial comments and the callback function should be removed and the keys should be wrapped in double quotes.

On Demand

Reserved Light

Reserved Medium

Reserved Heavy

Other

Previous endpoint: http://aws-assets-pricing-prod.s3.amazonaws.com/pricing/ec2/linux-od.js

Solution 3 - Amazon Ec2

using the aws cli (in the examples below, I have also included how the same thing can be executed using jq)

#to get a list of service codes:

aws pricing describe-services --region us-east-1

#to get a list of service codes (with jq):

aws pricing describe-services --region us-east-1 | jq -r '.Services[] | .ServiceCode'

which will return values like: >AmazonEC2
AmazonS3
AmazonRoute53
[...]



#to get a list of attributes for a given service code:

aws pricing describe-services --service-code AmazonEC2 --region us-east-1

#to get a list of attributes for a given service code (with jq):

aws pricing describe-services --service-code AmazonEC2 --region us-east-1 | jq -r '.Services[] | .AttributeNames[]'

which will return values like: >instancesku
location
memory
vcpu
volumeType
[...]



#to get pricing info now that you have a service code and attribute: (this will take a while since it is every sku for the service code, so I will show examples using filtering further down)

aws pricing get-products --service-code AmazonEC2 --region us-east-1

#to get pricing info now that you have a service code and attribute using a filter on instanceType and another 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

#to get pricing info now that you have a service code and attribute using a filter on instanceType and another for location (with 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'

which will return values like: > "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" [...]

Solution 4 - Amazon Ec2

In addition to @arturhoo's answer which provides the EC2 spots

You can obtain the historic prices with the CLI tool

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]'

which takes the spot price between 3:00am and 3:16am Monday 31st October 2016 (UTC)

[    [        "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"    ]
]

Solution 5 - Amazon Ec2

This ruby gem wraps the JSON pricing data provided by Amazon and provides a simple interface, which takes care of mapping the region and instance type names to the ones used in the EC2 API.

https://github.com/sonian/amazon-pricing

Solution 6 - Amazon Ec2

Beside the official AWS JSON endpoint, https://ec2.shop is also an option. It respond both to json and text based(so you can use grep, awk etc).

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

Also JSON like this:

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"
    }
  ]
}

Solution 7 - Amazon Ec2

If you are using golang, I wrote a library that can query the data using the "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/{offer_code}/current/index.{format}" format.

https://github.com/Chronojam/aws-pricing-api

import (
        "github.com/chronojam/aws-pricing-api/types/schema"
)

func main() {
        ec2 := &schema.AmazonEC2{}

        // Populate this object with new pricing data
        err := ec2.Refresh()
        if err != nil {
                panic(err)
        }

        // Get the price of all c4.Large instances,
        // running linux, on shared tenancy
        c4Large := []*schema.AmazonEC2_Product{}
        for _, p := range ec2.Products {
               if p.Attributes.InstanceType == "c4.large" &&
                       p.Attributes.OperatingSystem == "Linux" &&
                       p.Attributes.Tenancy == "Shared" {
                       c4Large = append(c4Large, p)
               }
        }
}

Solution 8 - Amazon Ec2

AWS has launched the new price list API for programming integration.

URL Syntax: > https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/{offer_code}/current/index.{format}

To get list of supporting services:

> https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json

AWS blog Referance: https://aws.amazon.com/blogs/aws/new-aws-price-list-api/

Solution 9 - Amazon Ec2

I am the author of an open-source tool called ec2-cost-calculate that will "report your EC2 instance usage on an hourly basis" - the tool is available at awsmissingtools.com. Output can be hourly, daily, monthly. Two versions of the tool exist, one written in Ruby and another written in bash.

Solution 10 - Amazon Ec2

As Amazon has recently changed the pricing scheme for EC2 instances (no more Medium or Light, only Heavy which has multiple payment options - allUpfront, partialUpfront, noUpfront) and also some time ago separated the old generation instances from the current ones, the list of undocumented pricing API links has changed as well the structure of JSON provisioned by these links. The full list if links of EC2 pricing undocumented API with descriptions, as well as the Python module for convenient access and structured output of pricing in JSON, CSV or Table formats can be found in the following repository:

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

Solution 11 - Amazon Ec2

If you're using Go, I wrote a package to decode the data into a struct, based on the files linked to in @okrasz's answer

https://github.com/recursionpharma/ec2prices

Feel free to contribute with more pricing data.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmorfysView Question on Stackoverflow
Solution 1 - Amazon Ec2okraszView Answer on Stackoverflow
Solution 2 - Amazon Ec2arturhooView Answer on Stackoverflow
Solution 3 - Amazon Ec2user138278View Answer on Stackoverflow
Solution 4 - Amazon Ec2Alexander McFarlaneView Answer on Stackoverflow
Solution 5 - Amazon Ec2Matt ZimmermanView Answer on Stackoverflow
Solution 6 - Amazon Ec2kureikainView Answer on Stackoverflow
Solution 7 - Amazon Ec2ChronojamView Answer on Stackoverflow
Solution 8 - Amazon Ec2MohanView Answer on Stackoverflow
Solution 9 - Amazon Ec2Colin JohnsonView Answer on Stackoverflow
Solution 10 - Amazon Ec2isemenovView Answer on Stackoverflow
Solution 11 - Amazon Ec2atpView Answer on Stackoverflow