get ec2 pricing programmatically?

后端 未结 11 613
借酒劲吻你
借酒劲吻你 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:05

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

提交回复
热议问题