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
pub mod errors;

#[cfg(feature = "acmpca")]
pub mod acmpca;

#[cfg(feature = "autoscaling")]
pub mod autoscaling;

#[cfg(feature = "cloudformation")]
pub mod cloudformation;

#[cfg(feature = "cloudwatch")]
pub mod cloudwatch;

#[cfg(feature = "ec2")]
pub mod ec2;

#[cfg(feature = "kms")]
pub mod kms;

#[cfg(feature = "s3")]
pub mod s3;

#[cfg(feature = "ssm")]
pub mod ssm;

#[cfg(feature = "sts")]
pub mod sts;

use std::io;

use aws_config::{self, meta::region::RegionProviderChain, timeout::TimeoutConfig};
use aws_types::{region::Region, SdkConfig as AwsSdkConfig};
use tokio::time::Duration;

/// Loads an AWS config from default environments.
pub async fn load_config(
    reg: Option<String>,
    operation_timeout: Option<Duration>,
) -> io::Result<AwsSdkConfig> {
    log::info!("loading AWS configuration for region {:?}", reg);
    let regp = RegionProviderChain::first_try(reg.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    let mut builder = TimeoutConfig::builder().connect_timeout(Duration::from_secs(5));
    if let Some(to) = &operation_timeout {
        if !to.is_zero() {
            builder = builder.operation_timeout(to.clone());
        }
    }
    let timeout_cfg = builder.build();

    let shared_config = aws_config::from_env()
        .region(regp)
        .timeout_config(timeout_cfg)
        .load()
        .await;
    Ok(shared_config)
}