Amazon Web Services (AWS) - Get Cost and Usage using the AWS CLI

by
Jeremy Canfield |
Updated: April 15 2024
| Amazon Web Services (AWS) articles
This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.
The aws ce get-cost-and-usage command can be used to return the total cost for a certain AWS service in a certain time frame. In this example, the S3 total cost was a whopping $0.026 for January of 2024!
~]$ aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-01-31 --granularity MONTHLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment --filter '{"Dimensions": {"Key": "SERVICE","Values": ["Amazon Simple Storage Service"]}}'
{
"GroupDefinitions": [
{
"Type": "DIMENSION",
"Key": "SERVICE"
},
{
"Type": "TAG",
"Key": "Environment"
}
],
"ResultsByTime": [
{
"TimePeriod": {
"Start": "2024-01-01",
"End": "2024-01-31"
},
"Total": {},
"Groups": [
{
"Keys": [
"Amazon Simple Storage Service",
"Environment$"
],
"Metrics": {
"BlendedCost": {
"Amount": "0.2621694181",
"Unit": "USD"
},
"UnblendedCost": {
"Amount": "0.2621694181",
"Unit": "USD"
},
"UsageQuantity": {
"Amount": "481081.9302019198",
"Unit": "N/A"
}
}
}
],
"Estimated": false
}
],
"DimensionValueAttributes": []
}
And here is an example to get the cost for your t3.small EC2 instances.
aws ce get-cost-and-usage \
--time-period Start=2024-01-01,End=2024-03-31 \
--granularity MONTHLY \
--metrics "BlendedCost" "UnblendedCost" "UsageQuantity" \
--group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment \
--filter '{ "Dimensions": { "Key": "INSTANCE_TYPE", "MatchOptions": ["EQUALS"], "Values": ["t3.small"] }}'
And here is an example to get the cost for EC2 - Other.
aws ce get-cost-and-usage \
--time-period Start=2024-01-01,End=2024-01-31 \
--granularity MONTHLY \
--metrics "BlendedCost" "UnblendedCost" "UsageQuantity" \
--group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment \
--filter '{"Dimensions": {"Key": "SERVICE","Values": ["EC2 - Other"]}}'
Did you find this article helpful?
If so, consider buying me a coffee over at