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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// 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),
}
}
}
// ── BillingProvider adapter ──────────────────────────────────────────────
use super::traits::{BillingProvider, RawBillItem};
use crate::service::CloudAccountConfig;
/// Adapter that implements [`BillingProvider`] for AWS.
pub struct AwsBillingAdapter {
client: AwsBillingClient,
}
impl AwsBillingAdapter {
/// Create an adapter from a [`CloudAccountConfig`].
///
/// This is async because `AwsBillingClient::new()` is async.
/// If explicit credentials are provided, uses them; otherwise falls back
/// to the default credential chain.
pub async fn from_config(config: &CloudAccountConfig) -> Result<Self> {
let region = config
.region
.clone()
.unwrap_or_else(|| "us-east-1".to_string());
let secret = config
.secret_access_key
.as_ref()
.or(config.access_key_secret.as_ref());
let client = if let (Some(ak), Some(sk)) = (&config.access_key_id, secret) {
AwsBillingClient::new(ak.clone(), sk.clone(), region).await?
} else {
AwsBillingClient::new_with_default_credentials(region).await?
};
Ok(Self { client })
}
}
impl BillingProvider for AwsBillingAdapter {
fn provider_name(&self) -> &'static str {
"aws"
}
fn currency(&self) -> &'static str {
"USD"
}
async fn query_bill_items(&self, billing_cycle: &str) -> Result<Vec<RawBillItem>> {
// Convert "2026-03" to date range
let start_date = format!("{}-01", billing_cycle);
let end_date = {
let parts: Vec<&str> = billing_cycle.split('-').collect();
let year: i32 = parts[0].parse().unwrap_or(2026);
let month: u32 = parts[1].parse().unwrap_or(1);
if month == 12 {
format!("{}-01-01", year + 1)
} else {
format!("{}-{:02}-01", year, month + 1)
}
};
let result = self
.client
.get_cost_and_usage(
&start_date,
&end_date,
"MONTHLY",
vec!["UnblendedCost"],
Some(vec![("DIMENSION", "SERVICE")]),
)
.await?;
let mut items = Vec::new();
if let Some(results) = result.results_by_time {
for period in results {
if let Some(groups) = period.groups {
for group in groups {
let service = group.keys.first().cloned().unwrap_or_default();
let cost = group
.metrics
.get("UnblendedCost")
.and_then(|v| v.get("Amount"))
.and_then(|v| v.as_str())
.and_then(|s| s.parse::<f64>().ok())
.unwrap_or(0.0);
items.push(RawBillItem {
product_name: service.clone(),
product_code: service,
cost,
region: String::new(),
instance_id: String::new(),
usage: None,
unit: None,
});
}
}
}
}
Ok(items)
}
async fn test_credentials(&self) -> Result<bool> {
self.client.check_access().await
}
}
#[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());
}
}