Chai
Chai

Reputation: 2026

AWS get-cost-and-usage by resource Tags

I am trying to get the costs for my aws resources by environment or usage. I setup my environments such that all resources (rds instances, vpc endpoints, ecs clusters and tasks etc etc) have a tag Environment: test or Environment: prod.

Now I want to explore the costs using aws cli:

aws ce get-cost-and-usage --time-period Start=2022-03-01,End=2022-04-09 --granularity MONTHLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity"   --filter file://costfilter.json

with `costfilter.json':

{
  "And": [
    {
      "Dimensions": {
        "Key": "REGION",
        "Values": ["us-east-2, eu-central-1"]
      }
    },
    {
      "Tags": {
        "Key": "Environment",
        "Values": ["test"]
      }
    }
  ]
}

I get no errors so syntactically it is fine, however it shows

DimensionValueAttributes: []
ResultsByTime:
- Estimated: false
  Groups: []
  TimePeriod:
    End: '2022-04-01'
    Start: '2022-03-01'
  Total:
    BlendedCost:
      Amount: '0'
      Unit: USD
    UnblendedCost:
      Amount: '0'
      Unit: USD
    UsageQuantity:
      Amount: '0'
      Unit: N/A
- Estimated: true
  Groups: []
  TimePeriod:
    End: '2022-04-09'
    Start: '2022-04-01'
  Total:
    BlendedCost:
      Amount: '0'
      Unit: USD
    UnblendedCost:
      Amount: '0'
      Unit: USD
    UsageQuantity:
      Amount: '0'
      Unit: N/A

Unfortunately the truth is that it is not all 0, so something is wrong here.

Is this the correct way to achieve what I want? If so what is missing? If not, how then do I approach it ?

ps. for instance alb elbv2 describe-tags for a certain lb will give me:

TagDescriptions:
- ResourceArn: arn:aws:elasticloadbalancing:eu-central-1:640753244498:loadbalancer/app/mosar-lb-test/3d7bd13953ee06b8
  Tags:
  - Key: Environment
    Value: test
  - Key: ManagedBy
    Value: ecs-cluster

So the tags really do exist.

Upvotes: 1

Views: 6117

Answers (1)

Daniel Seichter
Daniel Seichter

Reputation: 919

You did send the regions within your dimensions wrong:

"Values": ["us-east-2, eu-central-1"]

should be

"Values": ["us-east-2", "eu-central-1"]

Please also note, that the "current" date will often be 0, because of calculation of AWS itself. This is why we request in our cost monitoring tool all costs from a date in the past to "yesterday", to be sure, to have the full costs of the days.

To filter by tags, you also have those tags as "cost allocation tags", see https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html

Upvotes: 3

Related Questions