Query Builder

Query Builder

The Query Builder in Cost Explorer allows you to create custom cost analyses by combining filters, groupings, and date ranges.

Accessing Query Builder

  1. Navigate to Cost Explorer
  2. Click Query Builder or New Query
  3. Configure your query parameters
  4. Click Run Query

Query Components

Date Range

Select the time period for analysis:

OptionDescription
Current MonthMonth-to-date spending
Last MonthPrevious complete month
Last 3 MonthsQuarterly view
Last 6 MonthsHalf-year view
Last 12 MonthsAnnual view
Custom RangeSpecify exact dates

Filters

Narrow down results by dimension:

FilterDescription
ServiceAWS service (EC2, RDS, S3, Lambda, etc.)
AccountAWS account ID
RegionAWS region
Usage TypeSpecific usage categories
OperationAWS operation type
Instance TypeEC2/RDS instance type
Availability ZoneSpecific AZ
Resource IDIndividual resource identifier
TagCost allocation tags (prefix with tag:)

Multiple Filters

Combine filters for precise analysis:

Service = EC2
  AND Region = us-east-1
  AND Tag:Environment = production

Group By

Choose how to aggregate costs:

Group ByResult
ServiceCosts per AWS service
AccountCosts per AWS account
RegionCosts per region
TagCosts per tag value
Usage TypeDetailed usage breakdown

Granularity

Choose the time granularity for time series data:

GranularityDescription
HourlyHour-by-hour cost breakdown
DailyDay-by-day cost breakdown (default)
MonthlyMonth-by-month cost breakdown

Building a Query

Example 1: EC2 Costs by Region

  1. Date Range: Last Month
  2. Filters: Service = Amazon EC2
  3. Group By: Region
  4. Run Query

Example 2: Production Environment Costs

  1. Date Range: Last 3 Months
  2. Filters: Tag:Environment = production
  3. Group By: Service
  4. Run Query

Example 3: Account Comparison

  1. Date Range: Last 6 Months
  2. Filters: None (all accounts)
  3. Group By: Account, Month
  4. Run Query

Understanding Results

Table View

Results display in a table with:

ColumnDescription
DimensionGrouped values (service, account, etc.)
CostTotal cost for the period
% of TotalProportion of total spend
ChangeDifference from previous period

Chart View

Visualizations available:

  • Line Chart - Trends over time
  • Bar Chart - Comparative analysis
  • Pie Chart - Proportional breakdown
  • Stacked Area - Cumulative trends

Saving Queries

Save frequently-used queries for quick access:

  1. Configure your query
  2. Click Save Query
  3. Enter a name and description
  4. Access later from Saved Queries

Exporting Results

Download query results directly from your browser:

  1. Run your query
  2. Click Export or Download
  3. The CSV file downloads to your computer

Note: Export generates the CSV file in your browser from the displayed query results. For large datasets, consider narrowing your date range or filters.

CSV Format

date,service,region,cost
2024-01-01,EC2,us-east-1,1234.56
2024-01-01,RDS,us-east-1,789.01

Query Tips

Performance

  • Shorter date ranges run faster
  • Fewer filters = faster queries
  • Large accounts may have query limits

Accuracy

  • Data has 24-48 hour delay
  • Forecasts are estimates
  • Credits/refunds may appear later

Common Query Patterns

GoalFiltersGroup By
Monthly billingNoneMonth
Service breakdownNoneService
Team costsTag:Team = XService
Regional analysisNoneRegion
Account comparisonNoneAccount
Daily trendsLast 30 DaysDay

Troubleshooting

IssueCauseSolution
No resultsFilter too restrictiveBroaden filters
Slow queryLarge date rangeNarrow date range
Missing dataTag not activatedEnable cost allocation tag
Zero costsWrong filter valueVerify filter values