Arya Ray
Arya Ray

Reputation: 93

AWS CLI to fetch EC2 instance details

I am using the AWS CLI to fetching EC2 instance details. My requirements are:

1) Fetch only running EC2 instances:

However I can't able to fetch last 3 attributes (blockdevices name, platform and reserved or ondemand). When I add it in my query it is showing None. What do I need to change in query?

My query is:

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value,InstanceId,State.Name,InstanceType,Placement.AvailabilityZone]" --output table > output.txt

The output is:

|  test1 |  i-xxxxxxx  |  m1.small   |  running |  us-east-1a  |
|  test2 |  i-xxxxx    |  m1.large   |  running |  us-east-1c  |
|  test3 |  i-xxxxx    |  m1.xlarge  |  running |  us-east-1c  |
|  test4 |  i-xxxxxxx  |  m3.2xlarge |  running |  us-east-1a  |

2) Need to fetch each instance price considering those attributes (region,type,ondemand/reserved) and put full instance details in a CSV file.

Upvotes: 1

Views: 3359

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269101

Blockdevices name

I'm not sure what you mean by "name", but there is a DeviceName field:

aws ec2 describe-instances --query Reservations[*].Instances[*].BlockDeviceMappings[*].DeviceName

Platform

The Platform field is either Windows or undefined (for Linux):

aws ec2 describe-instances --query Reservations[*].Instances[*].Platform

Reserved or On-Demand

This is a billing concept and is not available for an instance. In fact, an instance could be billed as On-Demand in one hour and as a Reserved Instance in another hour.

The basic concept is that, for each Reserved Instance owned by an account, the billing system looks for a running Amazon EC2 instance that matches the Reserved Instance parameters. The matching instance is not charged the hourly fee for that hour. This information can be seen in the billing files, but is not available as information against the instance.

Make sure you have turned on Detailed Billing Reports so that you can receive this level of billing information. It is only available after the reports have been activated (not back in time).

Fetch the price

There is no command to fetch the price of a running Amazon EC2 instance. There is a Price List API that provides pricing information similar to the pricing pages on the AWS website.

You could write a program to fetch the prices via the Price List API and determine which price is appropriate for an instance. However, as stated above, the program would not know whether an instance was running as a Reserved Instance.

Put full instance details in a CSV file

The AWS CLI does not output CSV format. You would have to post-process the output into the desired format. Note that some of your information (eg BlockDevices) might return multiple values for a single instance.

Upvotes: 2

Related Questions