Skip to content

Commit ffd57c9

Browse files
Doug-AWSrcoh
andauthored
Updated SageMaker examples to take a region arg; added doc comments. (#524)
* Updated SageMaker examples to take a region arg. * Updated SageMaker code examples based on feedback Co-authored-by: Russell Cohen <rcoh@amazon.com>
1 parent 74bdc2b commit ffd57c9

File tree

6 files changed

+149
-67
lines changed

6 files changed

+149
-67
lines changed

aws/sdk/examples/sagemaker-helloworld/src/main.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

aws/sdk/examples/sagemaker-list-training-jobs/Cargo.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

aws/sdk/examples/sagemaker-list-training-jobs/src/main.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
[package]
2-
name = "sagemaker-helloworld"
2+
name = "sagemaker-code-examples"
33
version = "0.1.0"
4-
authors = ["Alistair McLean <mclean@amazon.com>"]
4+
authors = ["Alistair McLean <mclean@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
55
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
env_logger = "0.8.2"
1110
sagemaker = {package = "aws-sdk-sagemaker", path = "../../build/aws-sdk/sagemaker"}
11+
aws-types = { path = "../../build/aws-sdk/aws-types" }
12+
1213
tokio = { version = "1", features = ["full"] }
13-
14+
15+
env_logger = "0.8.2"
16+
chrono = "0.4.19"
17+
structopt = { version = "0.3", default-features = false }
18+
tracing-subscriber = "0.2.18"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
use aws_types::region::ProvideRegion;
7+
8+
use sagemaker::{Client, Config, Region};
9+
10+
use structopt::StructOpt;
11+
12+
#[derive(Debug, StructOpt)]
13+
struct Opt {
14+
/// The AWS Region. Overrides environment variable AWS_DEFAULT_REGION.
15+
#[structopt(short, long)]
16+
default_region: Option<String>,
17+
18+
/// Whether to display additional runtime information
19+
#[structopt(short, long)]
20+
verbose: bool,
21+
}
22+
23+
/// Lists the your SageMaker jobs in an AWS Region.
24+
/// /// # Arguments
25+
///
26+
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
27+
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
28+
/// If the environment variable is not set, defaults to **us-west-2**.
29+
/// * `[-v]` - Whether to display additional information.#[tokio::main]
30+
#[tokio::main]
31+
async fn main() -> Result<(), sagemaker::Error> {
32+
tracing_subscriber::fmt::init();
33+
34+
let Opt {
35+
default_region,
36+
verbose,
37+
} = Opt::from_args();
38+
39+
let region = default_region
40+
.as_ref()
41+
.map(|region| Region::new(region.clone()))
42+
.or_else(|| aws_types::region::default_provider().region())
43+
.unwrap_or_else(|| Region::new("us-west-2"));
44+
45+
if verbose {
46+
println!("SageMaker client version: {}", sagemaker::PKG_VERSION);
47+
println!("Region: {:?}", &region);
48+
println!();
49+
}
50+
51+
let conf = Config::builder().region(region).build();
52+
let client = Client::from_conf(conf);
53+
let job_details = client.list_training_jobs().send().await?;
54+
55+
println!("Job Name\tCreation DateTime\tDuration\tStatus");
56+
for j in job_details.training_job_summaries.unwrap_or_default() {
57+
let name = j.training_job_name.as_deref().unwrap_or_default();
58+
let creation_time = j.creation_time.unwrap().to_chrono();
59+
let training_end_time = j.training_end_time.unwrap().to_chrono();
60+
61+
let status = j.training_job_status.unwrap();
62+
let duration = training_end_time - creation_time;
63+
64+
println!(
65+
"{}\t{}\t{}\t{:#?}",
66+
name,
67+
creation_time.format("%Y-%m-%d@%H:%M:%S"),
68+
duration.num_seconds(),
69+
status
70+
);
71+
}
72+
73+
Ok(())
74+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
use aws_types::region::ProvideRegion;
7+
8+
use sagemaker::{Client, Config, Region};
9+
10+
use structopt::StructOpt;
11+
12+
#[derive(Debug, StructOpt)]
13+
struct Opt {
14+
/// The region. Overrides environment variable AWS_DEFAULT_REGION.
15+
#[structopt(short, long)]
16+
default_region: Option<String>,
17+
18+
/// Whether to display additional runtime information
19+
#[structopt(short, long)]
20+
verbose: bool,
21+
}
22+
23+
/// Lists the name, status, and type of your SageMaker instances in an AWS Region.
24+
/// /// # Arguments
25+
///
26+
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
27+
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
28+
/// If the environment variable is not set, defaults to **us-west-2**.
29+
/// * `[-v]` - Whether to display additional information.
30+
#[tokio::main]
31+
async fn main() -> Result<(), sagemaker::Error> {
32+
tracing_subscriber::fmt::init();
33+
34+
let Opt {
35+
default_region,
36+
verbose,
37+
} = Opt::from_args();
38+
39+
let region = default_region
40+
.as_ref()
41+
.map(|region| Region::new(region.clone()))
42+
.or_else(|| aws_types::region::default_provider().region())
43+
.unwrap_or_else(|| Region::new("us-west-2"));
44+
45+
if verbose {
46+
println!("SageMaker client version: {}", sagemaker::PKG_VERSION);
47+
println!("Region: {:?}", &region);
48+
}
49+
50+
let conf = Config::builder().region(region).build();
51+
let client = Client::from_conf(conf);
52+
let notebooks = client.list_notebook_instances().send().await?;
53+
54+
for n in notebooks.notebook_instances.unwrap_or_default() {
55+
let n_instance_type = n.instance_type.unwrap();
56+
let n_status = n.notebook_instance_status.unwrap();
57+
let n_name = n.notebook_instance_name.as_deref().unwrap_or_default();
58+
59+
println!(
60+
"Notebook Name : {}, Notebook Status : {:#?}, Notebook Instance Type : {:#?}",
61+
n_name, n_status, n_instance_type
62+
);
63+
}
64+
65+
Ok(())
66+
}

0 commit comments

Comments
 (0)