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
- Navigate to Cost Explorer
- Click Query Builder or New Query
- Configure your query parameters
- Click Run Query
Query Components
Date Range
Select the time period for analysis:
| Option | Description |
|---|---|
| Current Month | Month-to-date spending |
| Last Month | Previous complete month |
| Last 3 Months | Quarterly view |
| Last 6 Months | Half-year view |
| Last 12 Months | Annual view |
| Custom Range | Specify exact dates |
Filters
Narrow down results by dimension:
| Filter | Description |
|---|---|
| Service | AWS service (EC2, RDS, S3, Lambda, etc.) |
| Account | AWS account ID |
| Region | AWS region |
| Usage Type | Specific usage categories |
| Operation | AWS operation type |
| Instance Type | EC2/RDS instance type |
| Availability Zone | Specific AZ |
| Resource ID | Individual resource identifier |
| Tag | Cost allocation tags (prefix with tag:) |
Multiple Filters
Combine filters for precise analysis:
Service = EC2
AND Region = us-east-1
AND Tag:Environment = productionGroup By
Choose how to aggregate costs:
| Group By | Result |
|---|---|
| Service | Costs per AWS service |
| Account | Costs per AWS account |
| Region | Costs per region |
| Tag | Costs per tag value |
| Usage Type | Detailed usage breakdown |
Granularity
Choose the time granularity for time series data:
| Granularity | Description |
|---|---|
| Hourly | Hour-by-hour cost breakdown |
| Daily | Day-by-day cost breakdown (default) |
| Monthly | Month-by-month cost breakdown |
Building a Query
Example 1: EC2 Costs by Region
- Date Range: Last Month
- Filters: Service = Amazon EC2
- Group By: Region
- Run Query
Example 2: Production Environment Costs
- Date Range: Last 3 Months
- Filters: Tag:Environment = production
- Group By: Service
- Run Query
Example 3: Account Comparison
- Date Range: Last 6 Months
- Filters: None (all accounts)
- Group By: Account, Month
- Run Query
Understanding Results
Table View
Results display in a table with:
| Column | Description |
|---|---|
| Dimension | Grouped values (service, account, etc.) |
| Cost | Total cost for the period |
| % of Total | Proportion of total spend |
| Change | Difference 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:
- Configure your query
- Click Save Query
- Enter a name and description
- Access later from Saved Queries
Exporting Results
Download query results directly from your browser:
- Run your query
- Click Export or Download
- 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.01Query 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
| Goal | Filters | Group By |
|---|---|---|
| Monthly billing | None | Month |
| Service breakdown | None | Service |
| Team costs | Tag:Team = X | Service |
| Regional analysis | None | Region |
| Account comparison | None | Account |
| Daily trends | Last 30 Days | Day |
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| No results | Filter too restrictive | Broaden filters |
| Slow query | Large date range | Narrow date range |
| Missing data | Tag not activated | Enable cost allocation tag |
| Zero costs | Wrong filter value | Verify filter values |
Related Topics
- Cost Allocation Tags - Tag-based analysis
- Dashboard Widgets - Pre-built visualizations
- Cost Explorer Overview - Feature overview