get ec2 pricing programmatically?

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):

  • On-demand Linux: http://a0.awsstatic.com/pricing/1/ec2/linux-od.min.js
  • On-demand RedHat: http://a0.awsstatic.com/pricing/1/ec2/rhel-od.min.js
  • On-demand SUSE: http://a0.awsstatic.com/pricing/1/ec2/sles-od.min.js
  • On-demand Windows: http://a0.awsstatic.com/pricing/1/ec2/mswin-od.min.js
  • On-demand SQL Standard: http://a0.awsstatic.com/pricing/1/ec2/mswinSQL-od.min.js
  • On-demand SQL Web: http://a0.awsstatic.com/pricing/1/ec2/mswinSQLWeb-od.min.js
  • Reserved Linux: http://a0.awsstatic.com/pricing/1/ec2/ri-v2/linux-unix-shared.min.js
  • Reserved RedHat: http://a0.awsstatic.com/pricing/1/ec2/ri-v2/red-hat-enterprise-linux-shared.min.js
  • Reserved SUSE: http://a0.awsstatic.com/pricing/1/ec2/ri-v2/suse-linux-shared.min.js
  • Reserved Windows: http://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-shared.min.js
  • Reserved SQL Standard: http://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-with-sql-server-standard-shared.min.js
  • Reserved SQL Web: http://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-with-sql-server-web-shared.min.js
  • Reserved Spot instances: http://spot-price.s3.amazonaws.com/spot.js
  • Data transfer: http://a0.awsstatic.com/pricing/1/ec2/pricing-data-transfer-with-regions.min.js
  • EBS optimized: http://a0.awsstatic.com/pricing/1/ec2/pricing-ebs-optimized-instances.min.js
  • EBS: http://a0.awsstatic.com/pricing/1/ebs/pricing-ebs.min.js
  • Elastic IP: http://a0.awsstatic.com/pricing/1/ec2/pricing-elastic-ips.min.js
  • CloudWatch: http://a0.awsstatic.com/pricing/1/cloudwatch/pricing-cloudwatch.min.js
  • ELB: http://a0.awsstatic.com/pricing/1/ec2/pricing-elb.min.js
  • EMR: https://a0.awsstatic.com/pricing/1/emr/pricing-emr.min.js

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


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

  • Linux: http://a0.awsstatic.com/pricing/1/ec2/linux-od.min.js
  • Windows: http://a0.awsstatic.com/pricing/1/ec2/mswin-od.min.js
  • RHEL: http://a0.awsstatic.com/pricing/1/ec2/rhel-od.min.js
  • SLES: http://a0.awsstatic.com/pricing/1/ec2/sles-od.min.js
  • Windows w/ SQL Std: http://a0.awsstatic.com/pricing/1/ec2/mswinSQL-od.min.js
  • Windows w/ SQL Web: http://a0.awsstatic.com/pricing/1/ec2/mswinSQLWeb-od.min.js

Reserved Light

  • Linux: http://a0.awsstatic.com/pricing/1/ec2/linux-ri-light.min.js
  • Windows: http://a0.awsstatic.com/pricing/1/ec2/mswin-ri-light.min.js
  • RHEL: http://a0.awsstatic.com/pricing/1/ec2/rhel-ri-light.min.js
  • SLES: http://a0.awsstatic.com/pricing/1/ec2/sles-ri-light.min.js
  • Windows w/ SQL Std: http://a0.awsstatic.com/pricing/1/ec2/mswinSQL-ri-light.min.js
  • Windows w/ SQL Web: http://a0.awsstatic.com/pricing/1/ec2/mswinSQLWeb-ri-light.min.js

Reserved Medium

  • Linux: http://a0.awsstatic.com/pricing/1/ec2/linux-ri-medium.min.js
  • Windows: http://a0.awsstatic.com/pricing/1/ec2/mswin-ri-medium.min.js
  • RHEL: http://a0.awsstatic.com/pricing/1/ec2/rhel-ri-medium.min.js
  • SLES: http://a0.awsstatic.com/pricing/1/ec2/sles-ri-medium.min.js
  • Windows w/ SQL Std: http://a0.awsstatic.com/pricing/1/ec2/mswinSQL-ri-medium.min.js
  • Windows w/ SQL Web: http://a0.awsstatic.com/pricing/1/ec2/mswinSQLWeb-ri-medium.min.js

Reserved Heavy

  • Linux: http://a0.awsstatic.com/pricing/1/ec2/linux-ri-heavy.min.js
  • Windows: http://a0.awsstatic.com/pricing/1/ec2/mswin-ri-heavy.min.js
  • RHEL: http://a0.awsstatic.com/pricing/1/ec2/rhel-ri-heavy.min.js
  • SLES: http://a0.awsstatic.com/pricing/1/ec2/sles-ri-heavy.min.js
  • Windows w/ SQL Std: http://a0.awsstatic.com/pricing/1/ec2/mswinSQL-ri-heavy.min.js
  • Windows w/ SQL Web: http://a0.awsstatic.com/pricing/1/ec2/mswinSQLWeb-ri-heavy.min.js

Other

  • Spot Instances: http://spot-price.s3.amazonaws.com/spot.js
  • Data Transfer: http://a0.awsstatic.com/pricing/1/ec2/pricing-data-transfer-with-regions.min.js
  • EBS-Optimized Instances Surcharge: http://a0.awsstatic.com/pricing/1/ec2/pricing-ebs-optimized-instances.min.js
  • EBS: http://a0.awsstatic.com/pricing/1/ec2/pricing-ebs.min.js
  • Elastic IP: http://a0.awsstatic.com/pricing/1/ec2/pricing-elastic-ips.min.js
  • CloudWatch: http://a0.awsstatic.com/pricing/1/ec2/pricing-cloudwatch.min.js
  • ELB: http://a0.awsstatic.com/pricing/1/ec2/pricing-elb.min.js

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


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

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


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