I'm trying to rewrite my python inventory scripts into rust and ran into a problem using multiple aws profiles. I tried a simple task (well, in python it is simple) to list all available S3 buckets in all configured profiles, but got stuck with profiles. I'm looking in this direction, but just can't figure out how to implement it as there aren't many examples on this in the documentation.
I'm not sure if this is super supported by aws_config, but they do expose their parsing library so I was able to cobble some code together that works:
First, you need to load in the configuration files. I first did this without aws_config, but since aws config files don't exactly match .ini files, using aws_config is better as it obeys aws config file internal rules:
use aws_types;
use aws_runtime::env_config;
let fs = aws_types::os_shim_internal::Fs::real();
let env = aws_types::os_shim_internal::Env::real();
let profile_files = env_config::file::EnvConfigFiles::default();
let profiles_set = aws_config::profile::load(&fs, &env, &profile_files, None).await.unwrap();
Also, be sure to use this code inside a tokio enabled function (or another async library should work, tokio is just popular) as it uses some async functionality.
Then, depending on your use case, you can iteratively pass a profile into a client:
let section_names: Vec<String> = profiles_set
.profiles()
.map(|s| s.to_string())
.collect();
let config = aws_config::defaults(BehaviorVersion::latest())
.profile_name(section_names[0]); // makes this iterative in a for loop somewhere so you can change the name
let client = Client::new(&config);
I'm kinda surprised that this isn't supported natively in the AWS SDK for Rust. Thanks for sharing the code snippet. I needed this.
The AWS PowerShell module, which I use a lot, supports listing profiles easily. It's unfortunate when the AWS SDKs for different languages have differing feature sets.