Sub topic 13
Running cost queries from the AWS CLI
This chapter assumes you know how Cost Explorer groups and filters data, and that you have activated the Project and Environment tags. You also need the AWS CLI installed with credentials that allow ce:GetCostAndUsage. Basic command-line use is enough.
Why use the CLI
The console is good for exploring. The aws ce commands are better when you want a repeatable query, a script, or a report you can paste into a ticket. They call the same Cost Explorer API, so the numbers match the console.
Cost Explorer is a global service. Its endpoint is in us-east-1, and the CLI handles this for you.
Tagging and activating from the CLI
First, tag a resource. Then activate the tag keys. In an organization, run the activation from the management account.
aws ec2 create-tags \
--resources i-0123456789abcdef0 \
--tags Key=Project,Value=checkout Key=Environment,Value=prodaws ce update-cost-allocation-tags-status \
--cost-allocation-tags-status TagKey=Project,Status=Active TagKey=Environment,Status=ActiveConfirm that the activation took effect:
aws ce list-cost-allocation-tags --status ActiveA tag only affects cost data from the time it is activated. It does not rewrite earlier usage, and it can take up to 24 hours to appear in reports.
Querying cost by service
get-cost-and-usage needs a time period, a granularity and at least one metric.
aws ce get-cost-and-usage \
--time-period Start=2026-10-01,End=2026-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--group-by Type=DIMENSION,Key=SERVICEThe End date is exclusive. This query covers all of October 2026 and nothing from 1 November.
Querying cost by tag
To group by tag, change the group type to TAG.
aws ce get-cost-and-usage \
--time-period Start=2026-10-01,End=2026-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--group-by Type=TAG,Key=ProjectSpend with no Project tag comes back in a group with an empty tag value. That group is your untagged spend, so check its size first.
Filtering with a JSON file
Filters can be nested, so they are easier to keep in a file. This one limits a query to EC2 compute spend tagged Environment=prod.
{
"And": [
{ "Dimensions": { "Key": "SERVICE", "Values": ["Amazon Elastic Compute Cloud - Compute"] } },
{ "Tags": { "Key": "Environment", "Values": ["prod"] } }
]
}Save it as filter.json and pass it with --filter file://filter.json.
aws ce get-cost-and-usage \
--time-period Start=2026-10-01,End=2026-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--filter file://filter.jsonHow the pieces fit
Common errors
| Symptom | Likely cause |
|---|---|
| Tag group shows only an empty value | The tag key is not active yet, or the resources were not tagged in that period |
| `DataUnavailableException` | The period is outside the range Cost Explorer holds, or the account has no data yet |
| Totals are one day short | `End` is exclusive, so you set it a day too early |
| Access denied on activation | You ran it from a member account instead of the management account |
| Validation error on `--group-by` | You gave more than two group-by definitions, which is the limit |
Exercise
You run the tag-grouped query for October and the whole result sits in one group with an empty Project value, even though you tagged instances in September. What are two likely causes?
Show answer
The Project key may not be activated as a cost allocation tag, so Cost Explorer has no tag data for it. Or it was activated after October began, and activation does not apply to earlier usage. Check with aws ce list-cost-allocation-tags --status Active.