AWS Builder Center
How to check AWS costs using AWS CloudShell

How to check AWS costs using AWS CloudShell

Use a command in AWS CloudShell to quickly identify which services generated charges during the current month. Understand the results and how this approach compares with AWS Cost Explorer.

Entrepreneur & AWS Instructor at VocêCertificado®
Checking your AWS costs with CloudShell is a simple, quick alternative for identifying which services are generating charges in an AWS account. With a single command, you can generate an ASCII  table showing the costs accumulated during the month, grouped by service.
I use this type of query when I want a direct, minimalist view of all the services generating costs.
In this article, I will explain the differences between AWS Cost Explorer and AWS CloudShell, as well as show the results I obtained.

How AWS Cost Explorer helps you analyze costs

AWS Cost Explorer  allows you to visualize, filter, and analyze the costs and usage of your AWS account. This makes it easier to investigate trends, compare periods, and examine costs in greater detail.
The service allows you to group information by service, AWS Region, linked account, usage type, and other dimensions.
Example of AWS Cost Explorer with an EC2 instance filter
Example of AWS Cost Explorer with an EC2 instance filter
However, when I only want to quickly check which services generated charges during the month, I use a command-line query through AWS CloudShell.

Why use AWS CloudShell for a quick query

AWS CloudShell provides a terminal that you can access directly from the AWS Management Console. Another advantage is that the AWS CLI is already installed, and the credentials used to access the console are automatically made available in the CloudShell session.
This means you do not need to install the AWS CLI on your computer or configure credentials locally. Simply open CloudShell, paste the command, and wait for the results.
Regarding costs , CloudShell is available at no additional charge . However, you still pay for other AWS resources used or created through it, as well as standard data transfer rates.
Confirmation of the command pasted into AWS CloudShell
Confirmation of the command pasted into AWS CloudShell
In the example shown above, the query command uses the AWS Cost Explorer API , which charges $0.01 per paginated request.
Setting this cost aside, I personally find the report provided through AWS CloudShell useful and practical.

How to check AWS costs with CloudShell

Before running the command, AWS Cost Explorer must be enabled in the account, your identity must have the required access permission, and Cost Explorer access control must be enabled .
After that, open the AWS Management Console, search for CloudShell, and run:
1
2
3
4
5
6
7
8
aws ce get-cost-and-usage \
--time-period Start=$(date -u +%Y-%m-01),End=$(date -u -d '+1 day' +%Y-%m-%d) \
--granularity MONTHLY \
--metrics UnblendedCost \
--group-by Type=DIMENSION,Key=SERVICE \
--query 'ResultsByTime[0].Groups[?to_number(Metrics.UnblendedCost.Amount)!=`0`].[Keys[0],Metrics.UnblendedCost.Amount,Metrics.UnblendedCost.Unit]' \
--output table \
--no-cli-pager
This command uses the get-cost-and-usage  operation to query the data available in Cost Explorer.
To help you understand what each part does, here is a summary of the main points:
  • Start sets the first day of the current month.
  • End sets the following day as the end date.
  • MONTHLY combines the values for the selected period into a monthly result.
  • UnblendedCost returns the unblended cost for each service.
  • SERVICE groups the results by AWS service.
  • --query removes results with a value equal to zero and selects the service name, amount, and currency.
  • --output table organizes the information into a table.
  • --no-cli-pager displays the results directly in the terminal.
In my example result, I was able to quickly identify the services that generated costs.
Checking AWS costs with AWS CloudShell
Checking AWS costs with AWS CloudShell
It is important to understand that the purpose of this command is not to provide an inventory of active resources. It retrieves cost results grouped by service.

Cost Explorer and CloudShell complement each other

AWS Cost Explorer and AWS CloudShell do not replace each other in this context. Both query methods access the same Cost Explorer data, but through different approaches. One uses the visual interface, while the other uses a command that queries the API.
While AWS Cost Explorer provides a visual experience for investigating trends, comparing periods, and applying filters, AWS CloudShell provides a direct answer with a single command.
My choice to use AWS CloudShell was primarily motivated by speed, even though there is a cost that I consider relatively low for this type of query.
Although AWS Cost Explorer provides a visual interface, you need to configure the period, metric, grouping, and filters for the analysis. Through the terminal, I can obtain the same answer with a single command.
If you have never used AWS CloudShell, I challenge you to test this command in your account. Then, let me know in the comments which services appeared in your results and what your experience was like checking costs with AWS CloudShell.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article