Skip to content

Commit

Permalink
Moved Lambda code examples into Lambda directory (#481)
Browse files Browse the repository at this point in the history
* 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 <rcoh@amazon.com>
  • Loading branch information
Doug-AWS and rcoh committed Jun 22, 2021
1 parent ffd57c9 commit c346148
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 88 deletions.
14 changes: 0 additions & 14 deletions aws/sdk/examples/lambda-list-functions/Cargo.toml

This file was deleted.

51 changes: 0 additions & 51 deletions aws/sdk/examples/lambda-list-functions/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
println!("Lambda client version: {}", lambda::PKG_VERSION);
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();
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
Expand All @@ -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:
//
Expand Down
85 changes: 85 additions & 0 deletions aws/sdk/examples/lambda/src/bin/list-functions.rs
Original file line number Diff line number Diff line change
@@ -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<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 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);

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);
}
};
}

0 comments on commit c346148

Please sign in to comment.