Skip to content

Commit

Permalink
Updated SageMaker examples to take a region arg; added doc comments. (#…
Browse files Browse the repository at this point in the history
…524)

* Updated SageMaker examples to take a region arg.

* Updated SageMaker code examples based on feedback

Co-authored-by: Russell Cohen <rcoh@amazon.com>
  • Loading branch information
Doug-AWS and rcoh authored Jun 22, 2021
1 parent 74bdc2b commit ffd57c9
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 67 deletions.
24 changes: 0 additions & 24 deletions aws/sdk/examples/sagemaker-helloworld/src/main.rs

This file was deleted.

13 changes: 0 additions & 13 deletions aws/sdk/examples/sagemaker-list-training-jobs/Cargo.toml

This file was deleted.

26 changes: 0 additions & 26 deletions aws/sdk/examples/sagemaker-list-training-jobs/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
[package]
name = "sagemaker-helloworld"
name = "sagemaker-code-examples"
version = "0.1.0"
authors = ["Alistair McLean <mclean@amazon.com>"]
authors = ["Alistair McLean <mclean@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"

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

[dependencies]
env_logger = "0.8.2"
sagemaker = {package = "aws-sdk-sagemaker", path = "../../build/aws-sdk/sagemaker"}
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }


env_logger = "0.8.2"
chrono = "0.4.19"
structopt = { version = "0.3", default-features = false }
tracing-subscriber = "0.2.18"
74 changes: 74 additions & 0 deletions aws/sdk/examples/sagemaker/src/bin/list-training-jobs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

use sagemaker::{Client, Config, Region};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
/// The AWS Region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Lists the your SageMaker jobs in an AWS Region.
/// /// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.#[tokio::main]
#[tokio::main]
async fn main() -> Result<(), sagemaker::Error> {
tracing_subscriber::fmt::init();

let Opt {
default_region,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("SageMaker client version: {}", sagemaker::PKG_VERSION);
println!("Region: {:?}", &region);
println!();
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);
let job_details = client.list_training_jobs().send().await?;

println!("Job Name\tCreation DateTime\tDuration\tStatus");
for j in job_details.training_job_summaries.unwrap_or_default() {
let name = j.training_job_name.as_deref().unwrap_or_default();
let creation_time = j.creation_time.unwrap().to_chrono();
let training_end_time = j.training_end_time.unwrap().to_chrono();

let status = j.training_job_status.unwrap();
let duration = training_end_time - creation_time;

println!(
"{}\t{}\t{}\t{:#?}",
name,
creation_time.format("%Y-%m-%d@%H:%M:%S"),
duration.num_seconds(),
status
);
}

Ok(())
}
66 changes: 66 additions & 0 deletions aws/sdk/examples/sagemaker/src/bin/sagemaker-helloworld.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

use sagemaker::{Client, Config, Region};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
/// The region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Lists the name, status, and type of your SageMaker instances in an AWS Region.
/// /// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), sagemaker::Error> {
tracing_subscriber::fmt::init();

let Opt {
default_region,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("SageMaker client version: {}", sagemaker::PKG_VERSION);
println!("Region: {:?}", &region);
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);
let notebooks = client.list_notebook_instances().send().await?;

for n in notebooks.notebook_instances.unwrap_or_default() {
let n_instance_type = n.instance_type.unwrap();
let n_status = n.notebook_instance_status.unwrap();
let n_name = n.notebook_instance_name.as_deref().unwrap_or_default();

println!(
"Notebook Name : {}, Notebook Status : {:#?}, Notebook Instance Type : {:#?}",
n_name, n_status, n_instance_type
);
}

Ok(())
}

0 comments on commit ffd57c9

Please sign in to comment.