use anyhow::Result;
pub async fn build_aws_config(
profile: Option<String>,
region: String,
) -> Result<aws_config::SdkConfig> {
let mut builder = aws_config::defaults(aws_config::BehaviorVersion::latest());
if let Some(profile_name) = profile {
builder = builder.profile_name(profile_name);
}
builder = builder.region(aws_config::Region::new(region));
Ok(builder.load().await)
}
pub fn handle_aws_auth_error(err: anyhow::Error, profile: Option<String>) -> anyhow::Error {
let err_string = format!("{:?}", err);
if err_string.contains("ForbiddenException")
|| err_string.contains("AccessDenied")
|| err_string.contains("ExpiredToken")
|| err_string.contains("credentials")
|| err_string.contains("auth")
{
println!("AWS Authentication Error: Your credentials may be expired or insufficient.");
if let Some(profile_name) = profile {
println!("\nPlease run: aws sso login --profile {}", profile_name);
} else {
println!("\nPlease set valid AWS credentials or configure a profile.");
}
anyhow::anyhow!("Authentication failure")
} else {
err
}
}