1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2025 OpenObserve Inc.
//! AWS Cost Explorer Billing Provider
//!
//! This module implements billing integration with AWS using Cost Explorer API via official AWS SDK
use aws_config::{BehaviorVersion, Region};
use aws_sdk_costexplorer::{
types::{DateInterval, Granularity, GroupDefinition, Metric},
Client as CostExplorerClient,
};
use serde::{Deserialize, Serialize};
use super::super::{BillingError, Result};
/// AWS Billing Client using official AWS SDK
pub struct AwsBillingClient {
client: CostExplorerClient,
}
/// Response structures for compatibility with existing code
#[derive(Debug, Serialize, Deserialize)]
pub struct AwsCostResponse {
pub results_by_time: Option<Vec<AwsCostResult>>,
}
/// A single time-period result from the AWS Cost Explorer API.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AwsCostResult {
pub time_period: AwsTimePeriod,
pub total: Option<serde_json::Map<String, serde_json::Value>>,
pub groups: Option<Vec<AwsCostGroup>>,
pub estimated: Option<bool>,
}
/// Start/end date range for an AWS cost query.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AwsTimePeriod {
pub start: String,
pub end: String,
}
/// A grouped cost entry from the AWS Cost Explorer API.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AwsCostGroup {
pub keys: Vec<String>,
pub metrics: serde_json::Map<String, serde_json::Value>,
}
impl AwsBillingClient {
/// Create a new AWS Billing Client with explicit credentials
/// Note: Cost Explorer API is a global service that must use us-east-1 region
pub async fn new(
access_key_id: String,
secret_access_key: String,
_region: String, // Ignored - Cost Explorer requires us-east-1
) -> Result<Self> {
// AWS Cost Explorer is a global service that MUST use us-east-1
let ce_region = "us-east-1";
tracing::info!(
"Initializing AWS Cost Explorer client (forced region: {})",
ce_region
);
// Create credentials provider
let credentials = aws_sdk_costexplorer::config::Credentials::new(
access_key_id,
secret_access_key,
None, // session token
None, // expiry
"cloud-billing",
);
// Build AWS config with explicit credentials and us-east-1 region
let config = aws_config::defaults(BehaviorVersion::latest())
.credentials_provider(credentials)
.region(Region::new(ce_region.to_string()))
.load()
.await;
let client = CostExplorerClient::new(&config);
Ok(Self { client })
}
/// Create a new AWS Billing Client using default credential chain
/// This will automatically load credentials from:
/// 1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
/// 2. ~/.aws/credentials file
/// 3. IAM role (for EC2/ECS instances)
///
/// Note: Cost Explorer API is a global service that must use us-east-1 region
pub async fn new_with_default_credentials(_region: String) -> Result<Self> {
// AWS Cost Explorer is a global service that MUST use us-east-1
let ce_region = "us-east-1";
tracing::info!(
"Initializing AWS Cost Explorer client with default credential chain (forced region: {})",
ce_region
);
// Build AWS config using default credential chain
let config = aws_config::defaults(BehaviorVersion::latest())
.region(Region::new(ce_region.to_string()))
.load()
.await;
let client = CostExplorerClient::new(&config);
Ok(Self { client })
}
/// Query cost and usage data
pub async fn get_cost_and_usage(
&self,
start_date: &str,
end_date: &str,
granularity: &str,
metrics: Vec<&str>,
group_by: Option<Vec<(&str, &str)>>,
) -> Result<AwsCostResponse> {
tracing::info!(
"Querying AWS costs from {} to {} with granularity {}",
start_date,
end_date,
granularity
);
// Parse granularity
let granularity_enum = match granularity {
"DAILY" => Granularity::Daily,
"MONTHLY" => Granularity::Monthly,
"HOURLY" => Granularity::Hourly,
_ => {
return Err(BillingError::ServiceError(format!(
"Invalid granularity: {}",
granularity
)));
}
};
// Convert metrics to AWS SDK format
let metrics_enum: Vec<Metric> = metrics
.iter()
.filter_map(|m| match *m {
"BlendedCost" => Some(Metric::BlendedCost),
"UnblendedCost" => Some(Metric::UnblendedCost),
"UsageQuantity" => Some(Metric::UsageQuantity),
"AmortizedCost" => Some(Metric::AmortizedCost),
"NetAmortizedCost" => Some(Metric::NetAmortizedCost),
"NetUnblendedCost" => Some(Metric::NetUnblendedCost),
"NormalizedUsageAmount" => Some(Metric::NormalizedUsageAmount),
_ => {
tracing::warn!("Unknown metric: {}", m);
None
}
})
.collect();
if metrics_enum.is_empty() {
return Err(BillingError::ServiceError(
"No valid metrics provided".to_string(),
));
}
// Build the request
let mut request = self
.client
.get_cost_and_usage()
.time_period(
DateInterval::builder()
.start(start_date)
.end(end_date)
.build()
.map_err(|e| {
BillingError::ServiceError(format!("Failed to build date interval: {}", e))
})?,
)
.granularity(granularity_enum);
// Add metrics
for metric in metrics_enum {
request = request.metrics(metric.as_str());
}
// Add group by if provided
if let Some(group_by_items) = group_by {
for (typ, key) in group_by_items {
let group_def = GroupDefinition::builder()
.r#type(typ.into())
.key(key)
.build();
request = request.group_by(group_def);
}
}
// Execute the request
let response = request.send().await.map_err(|e| {
tracing::error!("AWS Cost Explorer API error details: {:?}", e);
BillingError::ServiceError(format!("AWS Cost Explorer API error: {}", e))
})?;
// Convert AWS SDK response to our response format
let results_by_time = response.results_by_time.map(|results| {
results
.into_iter()
.map(|result| {
// Convert time period
let time_period = AwsTimePeriod {
start: result
.time_period
.as_ref()
.map(|tp| tp.start.clone())
.unwrap_or_default(),
end: result
.time_period
.as_ref()
.map(|tp| tp.end.clone())
.unwrap_or_default(),
};
// Convert total metrics
let total = result.total.as_ref().map(|total_map| {
let mut map = serde_json::Map::new();
for (key, value) in total_map.iter() {
let mut metric_map = serde_json::Map::new();
if let Some(amount) = &value.amount {
metric_map.insert("Amount".to_string(), serde_json::json!(amount));
}
if let Some(unit) = &value.unit {
metric_map.insert("Unit".to_string(), serde_json::json!(unit));
}
map.insert(key.clone(), serde_json::json!(metric_map));
}
map
});
// Convert groups
let groups = result.groups.as_ref().map(|groups_vec| {
groups_vec
.iter()
.map(|group| {
let keys = group.keys.clone().unwrap_or_default();
let metrics = group
.metrics
.as_ref()
.map(|metrics_map| {
let mut map = serde_json::Map::new();
for (key, value) in metrics_map.iter() {
let mut metric_map = serde_json::Map::new();
if let Some(amount) = &value.amount {
metric_map.insert(
"Amount".to_string(),
serde_json::json!(amount),
);
}
if let Some(unit) = &value.unit {
metric_map.insert(
"Unit".to_string(),
serde_json::json!(unit),
);
}
map.insert(key.clone(), serde_json::json!(metric_map));
}
map
})
.unwrap_or_default();
AwsCostGroup { keys, metrics }
})
.collect()
});
let estimated = result.estimated;
AwsCostResult {
time_period,
total,
groups,
estimated: Some(estimated),
}
})
.collect()
});
Ok(AwsCostResponse { results_by_time })
}
/// Check if Cost Explorer API is accessible
pub async fn check_access(&self) -> Result<bool> {
// Try to query a small date range to verify access
let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
let yesterday = (chrono::Utc::now() - chrono::Duration::days(1))
.format("%Y-%m-%d")
.to_string();
match self
.get_cost_and_usage(&yesterday, &today, "DAILY", vec!["BlendedCost"], None)
.await
{
Ok(_) => Ok(true),
Err(_) => Ok(false),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
#[ignore] // Requires valid AWS credentials
async fn test_client_creation() {
let client = AwsBillingClient::new(
"test_key_id".to_string(),
"test_secret".to_string(),
"us-east-1".to_string(),
)
.await;
assert!(client.is_ok());
}
}