get ec2 pricing programmatically?

后端 未结 11 542
借酒劲吻你
借酒劲吻你 2020-11-30 18:22

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 monitorin

相关标签:
11条回答
  • 2020-11-30 19:21

    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

    0 讨论(0)
  • 2020-11-30 19:24

    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

    0 讨论(0)
  • 2020-11-30 19:26

    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.

    0 讨论(0)
  • 2020-11-30 19:27

    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.

    0 讨论(0)
  • 2020-11-30 19:28

    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)
                   }
            }
    }
    
    0 讨论(0)
提交回复
热议问题