Skip to content

Commit

Permalink
Update sts example with new SdkConfig loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Oct 4, 2023
1 parent 5a08878 commit 6276b03
Showing 1 changed file with 22 additions and 41 deletions.
63 changes: 22 additions & 41 deletions rust_dev_preview/examples/sts/src/bin/assume-role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,35 @@ struct Opt {

// Displays the STS AssumeRole Arn.
// snippet-start:[sts.rust.assume_role]
async fn assume_role(
config: &SdkConfig,
region: Region,
role_name: String,
session_name: Option<String>,
) -> Result<(), Error> {
match config.credentials_provider() {
Some(credential) => {
let provider = aws_config::sts::AssumeRoleProvider::builder(role_name)
.region(region)
.session_name(session_name.unwrap_or_else(|| String::from("rust-assume-role")))
.build(credential.clone());
let local_config = aws_config::from_env()
.credentials_provider(provider)
.load()
.await;
let client = Client::new(&local_config);
let req = client.get_caller_identity();
let resp = req.send().await;
match resp {
Ok(e) => {
println!("UserID : {}", e.user_id().unwrap_or_default());
println!("Account: {}", e.account().unwrap_or_default());
println!("Arn : {}", e.arn().unwrap_or_default());
}
Err(e) => println!("{:?}", e),
}
}
None => {
println!("No config provided");
async fn assume_role(config: &SdkConfig, role_name: String, session_name: Option<String>) {
let provider = aws_config::sts::AssumeRoleProvider::builder(role_name)
.session_name(session_name.unwrap_or("rust_sdk_example_session".into()))
.configure(config)
.build()
.await;

let local_config = aws_config::from_env()
.credentials_provider(provider)
.load()
.await;
let client = Client::new(&local_config);
let req = client.get_caller_identity();
let resp = req.send().await;
match resp {
Ok(e) => {
println!("UserID : {}", e.user_id().unwrap_or_default());
println!("Account: {}", e.account().unwrap_or_default());
println!("Arn : {}", e.arn().unwrap_or_default());
}
Err(e) => println!("{:?}", e),
}
Ok(())
}
// snippet-end:[sts.rust.assume_role]

/// Assumes another role and display some information about the role assumed
///
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
/// If not supplied, uses the value of the **AWS_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[--role-arn ROLE_ARN]` - The ARN of the IAM role to assume.
/// * `[--role-session-name ROLE_SESSION_NAME]` - The name of the session.
/// * `[-v]` - Whether to display information.
Expand Down Expand Up @@ -104,11 +90,6 @@ async fn main() -> Result<(), Error> {
}

let shared_config = aws_config::from_env().region(region_provider).load().await;
assume_role(
&shared_config,
shared_config.region().unwrap().clone(),
role_arn,
role_session_name,
)
.await
assume_role(&shared_config, role_arn, role_session_name).await;
Ok(())
}

0 comments on commit 6276b03

Please sign in to comment.