aws_manager/
lib.rs

1pub mod errors;
2
3#[cfg(feature = "acm")]
4pub mod acm;
5
6#[cfg(feature = "acmpca")]
7pub mod acmpca;
8
9#[cfg(feature = "autoscaling")]
10pub mod autoscaling;
11
12#[cfg(feature = "cloudformation")]
13pub mod cloudformation;
14
15#[cfg(feature = "cloudwatch")]
16pub mod cloudwatch;
17
18#[cfg(feature = "ec2")]
19pub mod ec2;
20
21#[cfg(feature = "kms")]
22pub mod kms;
23
24#[cfg(feature = "s3")]
25pub mod s3;
26
27#[cfg(feature = "sqs")]
28pub mod sqs;
29
30#[cfg(feature = "ssm")]
31pub mod ssm;
32
33#[cfg(feature = "sts")]
34pub mod sts;
35
36use aws_config::{self, meta::region::RegionProviderChain, timeout::TimeoutConfig};
37use aws_types::{region::Region, SdkConfig as AwsSdkConfig};
38use tokio::time::Duration;
39
40/// Loads an AWS config from default environments.
41pub async fn load_config(
42    region: Option<String>,
43    profile_name: Option<String>,
44    operation_timeout: Option<Duration>,
45) -> AwsSdkConfig {
46    log::info!("loading config for the region {:?}", region);
47
48    // if region is None, it automatically detects iff it's running inside the EC2 instance
49    let reg_provider = RegionProviderChain::first_try(region.map(Region::new))
50        .or_default_provider()
51        .or_else(Region::new("us-west-2"));
52
53    let mut builder = TimeoutConfig::builder().connect_timeout(Duration::from_secs(5));
54    if let Some(to) = &operation_timeout {
55        if !to.is_zero() {
56            builder = builder.operation_timeout(to.clone());
57        }
58    }
59    let timeout_cfg = builder.build();
60
61    let mut cfg = aws_config::from_env()
62        .region(reg_provider)
63        .timeout_config(timeout_cfg);
64    if let Some(p) = profile_name {
65        log::info!("loading the aws profile '{p}'");
66        cfg = cfg.profile_name(p);
67    }
68
69    cfg.load().await
70}