Reputation: 93
How to get ec2 instance details(like name,id,type,region,volume,platform,ondemand/reserved) with instance price details .
Using aws api in cli and write it as a csv file .
Thanks in advance .
Upvotes: 7
Views: 6007
Reputation: 2460
Expanding on @justsomeguy's answer, by playing around with TERM_MATCH
filters, I was able to programmatically pull the same lists of pricing I'm able to see on AWS's EC2 On Demand Pricing page (as opposed to only pulling pricing for a single instance type):
aws --region us-east-1 pricing get-products --service-code AmazonEC2 --filters \
"Type=TERM_MATCH,Field=capacitystatus,Value=Used" \
"Type=TERM_MATCH,Field=marketoption,Value=OnDemand" \
"Type=TERM_MATCH,Field=currentGeneration,Value=Yes" \
"Type=TERM_MATCH,Field=location,Value=Canada (Central)" \
"Type=TERM_MATCH,Field=operatingSystem,Value=Linux" \
"Type=TERM_MATCH,Field=marketoption,Value=OnDemand" \
"Type=TERM_MATCH,Field=servicecode,Value=AmazonEC2" \
"Type=TERM_MATCH,Field=tenancy,Value=Shared" \
"Type=TERM_MATCH,Field=operation,Value=RunInstances" \
| 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' > ~/Downloads/aws-ec2-pricing.csv
Upvotes: 2
Reputation: 559
similar to my answer here: get ec2 pricing programmatically?
you can do something similar to the following:
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'
Upvotes: 7
Reputation: 91
If your aim is not to automate the prizing of your servers, you can have a one shot from this URL: https://aws.amazon.com/fr/ec2/pricing/on-demand/
You'll need to know : server type (ex: m3.large) Reservation type (reserved or on demand) OS type (linux, windows, RHEL, ...) the hour coverage (it depends if you shutdown your server or not during the night or else ...)
Then you'll have a good approximation of the prize.
If you want to have more details, you'll have to have a look at your network and data activity. And this is not that easy to calculate...
Another approach would be to go in your pricing menu, and look at your facturation to know what you paid for the past month. But this won't work if you want to estimate the prize of a new server.
Hope it helped.
Upvotes: 2
Reputation: 91
I recommand you to use ansible
with the ec2-inventory
to do so.
Ansible will be able to take all thoses informations using request like:
Then you can have the platform like this for example :
ansible -i ec2.py -m debug -a "var=ec2_platform" all
You'll have to create a script in yaml to take the informations you need and write them in a csv file.
I don't know any easy way to get the exact price of the servers for amazon-ec2, there is a lot argument to take in account, the OS, the disk space, server type, is it reserved or not, etc ...
But I did a good approximation using what I told you above.
Here is the explanation for dynamic inventory with ansible and ec2: http://docs.ansible.com/ansible/intro_dynamic_inventory.html
Hope it helped !
Upvotes: 2