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

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: default avatarRussell Cohen <rcoh@amazon.com>
parent ffd57c93
Loading
Loading
Loading
Loading
+0 −14
Original line number Diff line number Diff line
[package]
name = "lambda-list-functions"
version = "0.1.0"
authors = ["Richard H. Boyd <rhboyd@amazon.com>"]
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" }
+2 −2
Original line number Diff line number Diff line
[package]
name = "lambda-invoke-function"
name = "lambda-code-examples"
version = "0.1.0"
authors = ["Richard H. Boyd <rhboyd@amazon.com>"]
authors = ["Richard H. Boyd <rhboyd@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
+46 −21
Original line number Diff line number Diff line
@@ -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<String>,

    /// 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:      {:?}", &region);
    if verbose {
        println!("Lambda client version: {}", lambda::PKG_VERSION);
        println!("Region:                {:?}", &region);
        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();
    }

    // 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:
        //
+85 −0
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@

use std::process;

// For command-line arguments.
use structopt::StructOpt;

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

use aws_types::region::ProvideRegion;
@@ -12,12 +15,38 @@ 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<String>,

    /// 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 region = aws_types::region::default_provider()
        .region()
    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:                {:?}", &region);

@@ -25,19 +54,24 @@ async fn main() {
            .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:");
            println!("Function ARNs:");

            let functions = resp.functions.unwrap_or_default();

            for function in &functions {
                println!("  {:?}", function.function_name);
                match &function.function_arn {
                    None => {}
                    Some(f) => {
                        println!("{}", f);
                    }
                }
            }

            println!("Found {} functions", functions.len());