From c34614864506509facfaa0e3bd392f342ffd2a5c Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 22 Jun 2021 12:02:44 -0700 Subject: [PATCH] Moved Lambda code examples into Lambda directory (#481) * Moved Lambda code examples into Lambda directory * Removed orphaned lambda-invoke-function/Cargo.toml * Delete unused lambda-list-functions cargo.toml Co-authored-by: Russell Cohen --- .../examples/lambda-list-functions/Cargo.toml | 14 --- .../lambda-list-functions/src/main.rs | 51 ----------- .../Cargo.toml | 4 +- .../src/bin/invoke-function.rs} | 67 ++++++++++----- .../examples/lambda/src/bin/list-functions.rs | 85 +++++++++++++++++++ 5 files changed, 133 insertions(+), 88 deletions(-) delete mode 100644 aws/sdk/examples/lambda-list-functions/Cargo.toml delete mode 100644 aws/sdk/examples/lambda-list-functions/src/main.rs rename aws/sdk/examples/{lambda-invoke-function => lambda}/Cargo.toml (79%) rename aws/sdk/examples/{lambda-invoke-function/src/main.rs => lambda/src/bin/invoke-function.rs} (76%) create mode 100644 aws/sdk/examples/lambda/src/bin/list-functions.rs diff --git a/aws/sdk/examples/lambda-list-functions/Cargo.toml b/aws/sdk/examples/lambda-list-functions/Cargo.toml deleted file mode 100644 index 711cc3959e..0000000000 --- a/aws/sdk/examples/lambda-list-functions/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "lambda-list-functions" -version = "0.1.0" -authors = ["Richard H. Boyd "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -lambda = { package = "aws-sdk-lambda", path = "../../build/aws-sdk/lambda" } -tokio = { version = "1", features = ["full"] } -structopt = { version = "0.3", default-features = false } -tracing-subscriber = { version = "0.2.16", features = ["fmt"] } -aws-types = { path = "../../build/aws-sdk/aws-types" } diff --git a/aws/sdk/examples/lambda-list-functions/src/main.rs b/aws/sdk/examples/lambda-list-functions/src/main.rs deleted file mode 100644 index 6753238aca..0000000000 --- a/aws/sdk/examples/lambda-list-functions/src/main.rs +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0. - */ - -use std::process; - -use lambda::{Client, Config, Region}; - -use aws_types::region::ProvideRegion; - -use tracing_subscriber::fmt::format::FmtSpan; -use tracing_subscriber::fmt::SubscriberBuilder; - -#[tokio::main] -async fn main() { - let region = aws_types::region::default_provider() - .region() - .unwrap_or_else(|| Region::new("us-west-2")); - - println!("Lambda client version: {}", lambda::PKG_VERSION); - println!("Region: {:?}", ®ion); - - SubscriberBuilder::default() - .with_env_filter("info") - .with_span_events(FmtSpan::CLOSE) - .init(); - - let config = Config::builder().region(region).build(); - - let client = Client::from_conf(config); - - match client.list_functions().send().await { - Ok(resp) => { - println!("Functions:"); - - let functions = resp.functions.unwrap_or_default(); - - for function in &functions { - println!(" {:?}", function.function_name); - } - - println!("Found {} functions", functions.len()); - } - Err(e) => { - println!("Got an error listing functions:"); - println!("{}", e); - process::exit(1); - } - }; -} diff --git a/aws/sdk/examples/lambda-invoke-function/Cargo.toml b/aws/sdk/examples/lambda/Cargo.toml similarity index 79% rename from aws/sdk/examples/lambda-invoke-function/Cargo.toml rename to aws/sdk/examples/lambda/Cargo.toml index 24ab408322..197ca11106 100644 --- a/aws/sdk/examples/lambda-invoke-function/Cargo.toml +++ b/aws/sdk/examples/lambda/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "lambda-invoke-function" +name = "lambda-code-examples" version = "0.1.0" -authors = ["Richard H. Boyd "] +authors = ["Richard H. Boyd ", "Doug Schwartz "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/aws/sdk/examples/lambda-invoke-function/src/main.rs b/aws/sdk/examples/lambda/src/bin/invoke-function.rs similarity index 76% rename from aws/sdk/examples/lambda-invoke-function/src/main.rs rename to aws/sdk/examples/lambda/src/bin/invoke-function.rs index cbd9487f7a..91b399b699 100644 --- a/aws/sdk/examples/lambda-invoke-function/src/main.rs +++ b/aws/sdk/examples/lambda/src/bin/invoke-function.rs @@ -11,6 +11,9 @@ // types from the Rust standard library use std::{process, str}; +// For command-line arguments. +use structopt::StructOpt; + // types from the AWS SDK for Rust use aws_types::region::ProvideRegion; use lambda::{error::InvokeErrorKind, Client, Config, Region, SdkError}; @@ -19,26 +22,53 @@ use lambda::{error::InvokeErrorKind, Client, Config, Region, SdkError}; use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::SubscriberBuilder; +#[derive(Debug, StructOpt)] +struct Opt { + /// The region. Overrides environment variable AWS_DEFAULT_REGION. + #[structopt(short, long)] + default_region: Option, + + /// Specifies the Lambda function's ARN + #[structopt(short, long)] + arn: String, + + /// Whether to display additional runtime information + #[structopt(short, long)] + verbose: bool, +} + +/// Invokes a Lambda function by its ARN. +/// # Arguments +/// +/// * `-a ARN` - The ARN of the Lambda function. +/// * `[-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() { - // We are going to attempt to load the AWS region from the default provider - // or else we will default to the us-west-2 region (also referred to as - // `PDX`). - let region = aws_types::region::default_provider() - .region() + let Opt { + arn, + 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")); - // My favorite part about learning a new language is to use print - // statements to confirm the program is doing what I think it's doing. - // Let's borrow that region information so we can print it. - println!("Region: {:?}", ®ion); - println!("Lambda client version: {}", lambda::PKG_VERSION); + if verbose { + println!("Lambda client version: {}", lambda::PKG_VERSION); + println!("Region: {:?}", ®ion); + println!("Lambda function ARN: {}", arn); - // Your guess is as good as mine what this does. - SubscriberBuilder::default() - .with_env_filter("info") - .with_span_events(FmtSpan::CLOSE) - .init(); + SubscriberBuilder::default() + .with_env_filter("info") + .with_span_events(FmtSpan::CLOSE) + .init(); + } // The AWS SDK for Rust service clients can be instantiated in a few // different ways. The way we're instantiating it here is to first build @@ -64,12 +94,7 @@ async fn main() { // // We are going to use the full ARN to prevent any ambiguity in which // function will be invoked. - match client - .invoke() - .function_name("arn:aws:lambda:us-west-2:072326518754:function:hello-python") - .send() - .await - { + match client.invoke().function_name(arn).send().await { // If the API call returns without an error, the Lambda Invoke API // returns a response object containing the following: // diff --git a/aws/sdk/examples/lambda/src/bin/list-functions.rs b/aws/sdk/examples/lambda/src/bin/list-functions.rs new file mode 100644 index 0000000000..ccb9230edf --- /dev/null +++ b/aws/sdk/examples/lambda/src/bin/list-functions.rs @@ -0,0 +1,85 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +use std::process; + +// For command-line arguments. +use structopt::StructOpt; + +use lambda::{Client, Config, Region}; + +use aws_types::region::ProvideRegion; + +use tracing_subscriber::fmt::format::FmtSpan; +use tracing_subscriber::fmt::SubscriberBuilder; + +#[derive(Debug, StructOpt)] +struct Opt { + /// The region. Overrides environment variable AWS_DEFAULT_REGION. + #[structopt(short, long)] + default_region: Option, + + /// Whether to display additional runtime information + #[structopt(short, long)] + verbose: bool, +} + +/// Lists the ARNs of your Lambda functions. +/// # 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() { + 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!("Lambda client version: {}", lambda::PKG_VERSION); + println!("Region: {:?}", ®ion); + + SubscriberBuilder::default() + .with_env_filter("info") + .with_span_events(FmtSpan::CLOSE) + .init(); + } + + let config = Config::builder().region(region).build(); + let client = Client::from_conf(config); + + match client.list_functions().send().await { + Ok(resp) => { + println!("Function ARNs:"); + + let functions = resp.functions.unwrap_or_default(); + + for function in &functions { + match &function.function_arn { + None => {} + Some(f) => { + println!("{}", f); + } + } + } + + println!("Found {} functions", functions.len()); + } + Err(e) => { + println!("Got an error listing functions:"); + println!("{}", e); + process::exit(1); + } + }; +}