2

The aws web console under "Billing" (https://console.aws.amazon.com/billing/) shows "Month-to-Date Spend by Service" which breaks down costs incurred so far this month by each service that is in use by an account.

It also shows the "Spend Summary" which shows the recent monthly total incurred cost as well as this month's forecast.

Is there a way to retrieve this information using the AWS CLI?

1 Answer 1

1

yes it is, I made a lambda function that posted it to a slack channel.

This is the CLI reference ce = cost-explorer

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/index.html

I remember there was a tricky part and if I'm not mistaken the forecast would be from today to the end of the month, so as the days passed it would decrease. When I built my version we were doing a lot of cost optimizations so it was very deceiving hahaha

So if you want the cost forecast to match the dashboard, you have to get the summary + the forecast. Not something super tricky, just something to watch for.

3
  • It looks like the cost explorer API can be queried for detailed usage statistics associated with arbitrary time windows. My question is specifically how to get exactly what the dashboard shows using the aws cli.
    – webninja
    Commented Nov 20, 2021 at 10:23
  • Also, I the cost explorer is a separate service that must be enabled. The pricing page says each cost explorer request will incur a cost.
    – webninja
    Commented Nov 20, 2021 at 10:39
  • sorry, I didn't make it clear. you can get the information but you will have to compose it using several API calls. Think of it like 'I want to get all the information I see in the VPC dashboard'. You will have to list the vpcs, aggregate the submets, etc. Same thing with the cost explorer, it's possible but there is no 'get-dash-stats' method, you have to compose it. That's what I meant by the monthly estimate, not even that, which is supposed to be trivial is a straightforward API call.
    – Magus
    Commented Nov 20, 2021 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.