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
use aws_config::{
    meta::region::RegionProviderChain, profile::ProfileFileCredentialsProvider,
    provider_config::ProviderConfig,
};
use aws_sdk_lambda::{Client, Region, RetryConfig};
use clap::Args;

const DEFAULT_REGION: &str = "us-east-1";

#[derive(Args, Clone, Debug)]
pub struct RemoteConfig {
    /// AWS configuration profile to use for authorization
    #[clap(short, long)]
    pub profile: Option<String>,

    /// AWS region to deploy, if there is no default
    #[clap(short, long)]
    pub region: Option<String>,

    /// AWS Lambda alias to associate the function to
    #[clap(short, long)]
    pub alias: Option<String>,

    /// Number of attempts to try failed operations
    #[clap(long, default_value = "1")]
    retry_attempts: u32,
}

/// Initialize an AWS Lambda client.
/// Uses us-east-1 as the default region if no region is provided.
pub async fn init_client(config: &RemoteConfig) -> Client {
    let region_provider = RegionProviderChain::first_try(config.region.clone().map(Region::new))
        .or_default_provider()
        .or_else(Region::new(DEFAULT_REGION));
    let region = region_provider.region().await;

    let mut config_loader = aws_config::from_env()
        .region(region_provider)
        .retry_config(RetryConfig::default().with_max_attempts(config.retry_attempts));

    if let Some(profile) = &config.profile {
        let conf = ProviderConfig::without_region().with_region(region);
        let creds_provider = ProfileFileCredentialsProvider::builder()
            .profile_name(profile)
            .configure(&conf)
            .build();
        config_loader = config_loader.credentials_provider(creds_provider);
    }

    let config = config_loader.load().await;
    Client::new(&config)
}

pub use aws_sdk_lambda;