Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 270
Leo Avatar asked Oct 27 '25 04:10

Leo


1 Answers

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

like image 183
John Avatar answered Oct 29 '25 20:10

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!