Leo
Leo

Reputation: 862

How to get cost for each EC2, not total cost for all EC2 from AWS API

I'm studying AWS api to retrieve requisite information about my EC2 instances.

So, I'm on AWS Cost Explorer Service.

It has function 'GetCostAndUsage' that, for example, sends information below. (this is an example from official AWS document)

{
"TimePeriod": {
"Start":"2017-09-01",
"End": "2017-10-01"
},
"Granularity": "MONTHLY",
"Filter": {      
"Dimensions": {
  "Key": "SERVICE",
  "Values": [
    "Amazon Simple Storage Service"
  ]
}
},
"GroupBy":[
{
  "Type":"DIMENSION",
  "Key":"SERVICE"
},
{
  "Type":"TAG",
  "Key":"Environment"
}
],
 "Metrics":["BlendedCost", "UnblendedCost", "UsageQuantity"]
}

and retrieve information below. (this is an example from official AWS document)

{
"GroupDefinitions": [
{
  "Key": "SERVICE",
  "Type": "DIMENSION"
},
{
  "Key": "Environment",
  "Type": "TAG"
}
],
"ResultsByTime": [
{
  "Estimated": false,
  "Groups": [
    {
      "Keys": [
        "Amazon Simple Storage Service",
        "Environment$Prod"
      ],
      "Metrics": {
        "BlendedCost": {
          "Amount": "39.1603300457",
          "Unit": "USD"
        },
        "UnblendedCost": {
          "Amount": "39.1603300457",
          "Unit": "USD"
        },
        "UsageQuantity": {
          "Amount": "173842.5440074444",
          "Unit": "N/A"
        }
      }
    },
    {
      "Keys": [
        "Amazon Simple Storage Service",
        "Environment$Test"
      ],
      "Metrics": {
        "BlendedCost": {
          "Amount": "0.1337464807",
          "Unit": "USD"
        },
        "UnblendedCost": {
         "Amount": "0.1337464807",
          "Unit": "USD"
        },
        "UsageQuantity": {
          "Amount": "15992.0786663399",
          "Unit": "N/A"
        }
      }
    }
  ],
  "TimePeriod": {
    "End": "2017-10-01",
    "Start": "2017-09-01"
  },
  "Total": {}
}
]
}

The retrieved data in key 'Metrics' I guess, it is total cost. not each. So, How can I get each usage and cost for each EC2 instance??

Upvotes: 4

Views: 5156

Answers (4)

John
John

Reputation: 61

This was way harder than I had imagined so I'm sharing in case someone else needs it.

aws ce get-cost-and-usage \
    --filter file://filters.json \
    --time-period Start=2021-08-01,End=2021-08-14 \
    --granularity DAILY \
    --metrics "BlendedCost" \
    --group-by Type=TAG,Key=Name

Contents of filters.json:

{
    "Dimensions": {
        "Key": "SERVICE",
        "Values": [
            "Amazon Elastic Compute Cloud - Compute"
        ]
    }
}

--- Available Metrics ---
AmortizedCost
BlendedCost
NetAmortizedCost
NetUnblendedCost
NormalizedUsageAmount
UnblendedCost
UsageQuantity

Descriptions for most metrics except for usage: https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/ce-advanced.html

Upvotes: 6

xxyjoel
xxyjoel

Reputation: 591

Know this question is old, but you will need to use the GetCostAndUsageWithResources call, as opposed to GetCostAndUsage.

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/get-cost-and-usage-with-resources.html

Upvotes: 2

Miguel Conde
Miguel Conde

Reputation: 853

You can use tags to track the cost of resources. In the case of EC2 you can assign tags like Project: myprojcet or Application: myapp and in cost explorer then filter the expenses by tags and use the tag that has been put to track the expenses. If the instance at some point was covered by a reservation plan, the tag will only show you the cost of the period in which your expenses were not covered.

Upvotes: 0

E.J. Brennan
E.J. Brennan

Reputation: 46849

It's going to be difficult to associate an exact cost with each instance - simple example, you have 2 instances of the same size - one reserved and one on-demand - you run both for 1/2 the month and then turn off one of them for the second 1/2 of the month.

You will pay for a reserved instance for the entire month and an on-demand instance for 1/2 the month - but which instance was reserved and which was on-demand? You can't tell; the concept of a reserved instance is just a billing concept, and is not associated with a particular instance.

You might be able to approximate what you are looking for - but there are limitations.

Upvotes: 0

Related Questions