Unverified Commit ffd57c93 authored by Doug's avatar Doug Committed by GitHub
Browse files

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: default avatarRussell Cohen <rcoh@amazon.com>
parent 74bdc2b4
Loading
Loading
Loading
Loading
+0 −13
Original line number Diff line number Diff line
[package]
name = "sagemaker-list-training-jobs"
version = "0.1.0"
authors = ["Alistair McLean <mclean@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"}
tokio = { version = "1", features = ["full"] }
chrono = "0.4.19"
 No newline at end of file
+9 −4
Original line number Diff line number Diff line
[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"
 No newline at end of file
+74 −0
Original line number Diff line number Diff line
/*
 * 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> {
    let client = sagemaker::Client::from_env();
    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");
@@ -12,14 +61,13 @@ async fn main() -> Result<(), sagemaker::Error> {
        let status = j.training_job_status.unwrap();
        let duration = training_end_time - creation_time;

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

    Ok(())
+66 −0
Original line number Diff line number Diff line
@@ -3,9 +3,52 @@
 * 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> {
    let client = sagemaker::Client::from_env();
    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() {
@@ -13,11 +56,10 @@ async fn main() -> Result<(), sagemaker::Error> {
        let n_status = n.notebook_instance_status.unwrap();
        let n_name = n.notebook_instance_name.as_deref().unwrap_or_default();

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

    Ok(())
Loading